logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Paul Rayman  
#1 Posted : Monday, April 16, 2018 6:35:12 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 678

Thanks: 1 times
Was thanked: 81 time(s) in 80 post(s)
Q: Is it possible to produce PDF document using Pdfium?

A: Yes, it is possible.
For example the following c# code produce document like this
quotation.pdf (16kb) downloaded 15 time(s).
quotation.png (49kb) downloaded 35 time(s).

Code:

    public void CreateDocument()
    {
        //All coordinates in this method are in inches

        var doc = PdfDocument.CreateNew();
        var page = AddNewPage(doc, 8.27f, 11.69f);
        PdfFont calibryBold = CreateFont(page, true, false, "Calibri");
        PdfFont calibryItalic = CreateFont(page, false, true, "Calibri");
        PdfFont calibry = CreateFont(page, false, false, "Calibri");

        //Insert logo from file
        using (Bitmap logo = Bitmap.FromFile(@"d:\0\logo_square.png") as Bitmap)
        {
            InsertImageAt(page, 0.5f, 10.07f, 1.12f, 1.12f, logo);
        }
        InsertText(page, "Patagames Software", 7.69f, 11.02f, calibryBold, Color.Black, 11.04f, true);
        InsertText(page, "sales@patagames.com", 7.69f, 10.83f, calibryBold, Color.SteelBlue, 11.04f, true);
        InsertText(page, "https://patagames.com", 7.69f, 10.65f, calibryBold, Color.SteelBlue, 11.04f, true);
        InsertText(page, DateTime.Now.ToString("MMM. dd, yyyy"), 7.69f, 10.30f, calibryBold, Color.Black, 11.04f, true);
        InsertText(page, "#1804051", 7.69f, 10.11f, calibryBold, Color.Black, 11.04f, true);
        InsertText(page, "Quotation", 3.38f, 9.37f, calibryBold, Color.Black, 26.04f);
        InsertText(page, "Dear %username%,", 0.51f, 9.02f, calibry, Color.Black);
        InsertText(page, "We have pleasure in submitting our quotation as requested. Please contact us should you have any questions at all.", 0.51f, 8.79f, calibry, Color.Black);
        InsertText(page, "Details", 0.51f, 7.97f, calibryItalic, Color.Black, 12.96f);
        InsertText(page, "Quantity", 4.96f, 7.97f, calibryItalic, Color.Black, 12.96f);
        InsertText(page, "Price", 6.20f, 7.97f, calibryItalic, Color.Black, 12.96f);
        InsertText(page, "Amount", 6.86f, 7.97f, calibryItalic, Color.Black, 12.96f);
        //deaw line at the top edge
        InsertRect(page, 0.41f, 10.0600f, 7.69f, 10.0601f, Color.Black);
        //draw table background
        InsertRect(page, 4.88f, 7.47f, 7.67f, 7.91f, Color.WhiteSmoke);
        InsertRect(page, 4.88f, 7.03f, 7.67f, 7.25f, Color.WhiteSmoke);
        //draw table lines
        InsertRect(page, 0.43f, 7.9100f, 7.67f, 7.9101f, Color.Black);
        InsertRect(page, 4.8700f, 7.91f, 4.8801f, 7.03f, Color.Black);

        InsertText(page, "Pdfium.Net SDK", 4.8f , 7.49f, calibryItalic, Color.Black, 12.96f, true);
        InsertText(page, "Tesseract.Net SDK", 4.8f, 7.27f, calibryItalic, Color.Black, 12.96f, true);

        InsertText(page, "15", 5.44f, 7.51f, calibry, Color.Black, 11.04f, true);
        InsertText(page, "1", 5.44f, 7.29f, calibry, Color.Black, 11.04f, true);

        InsertText(page, "400 USD", 6.52f, 7.51f, calibry, Color.Black, 11.04f, true);
        InsertText(page, "450 USD", 6.52f, 7.29f, calibry, Color.Black, 11.04f, true);
        InsertText(page, "Total", 6.52f, 7.11f, calibryBold, Color.Black, 11.04f, true);

        InsertText(page, "6000 USD", 7.6f, 7.51f, calibry, Color.Black, 11.04f, true);
        InsertText(page, "450 USD", 7.6f, 7.29f, calibry, Color.Black, 11.04f, true);
        InsertText(page, "6450 USD", 7.6f, 7.11f, calibryBold, Color.Black, 11.04f, true);

        InsertText(page, "Kind regards", 0.51f, 6.55f, calibryBold, Color.Black);
        InsertText(page, "Tany Olivo", 0.51f, 6.35f, calibryBold, Color.Black);
        InsertText(page, "Sales executive for Patagames.", 0.51f, 6.14f, calibryBold, Color.Black);

        InsertText(page, "Please visit the following link for ordering", 0.51f, 5.35f, calibry, Color.Gray);
        InsertText(page, "https://secure.avangate.com/order/checkout.php?PRODS=4657591,4659535&QTY=15,1&CART=1&CARD=1", 0.51f, 5.12f, calibry, Color.SteelBlue);

        //Insert qrcode from file
        using (Bitmap qrcode = Bitmap.FromFile(@"d:\0\qrcode.png") as Bitmap)
        {
            InsertImageAt(page, 0.5f, 3.69f, 1.19f, 1.19f, qrcode);
        }

        page.GenerateContent();

        doc.Save(@"c:\test_quote.pdf", SaveFlags.NoIncremental);
    }



    //----------------------------------------------------------------------------------------------------------



    private PdfPage AddNewPage(PdfDocument doc, float Width, float Height)
    {
        //PDF point is a 1/72 enches
        doc.Pages.InsertPageAt(doc.Pages.Count, Width * 72, Height * 72);
        return doc.Pages[doc.Pages.Count - 1];
    }

    /*private PdfFont CreateFont(PdfPage page, bool isBold, bool isItalic, string fontName)
    {
        LOGFONT lf = new LOGFONT() { lfCharSet = LOGFONT.FontCharSet.ANSI_CHARSET, lfFaceName = fontName, lfItalic = isItalic, lfPitchAndFamily = LOGFONT.FontPitchAndFamily.DEFAULT_PITCH, lfWeight = isBold ? LOGFONT.FontWeight.FW_SEMIBOLD : LOGFONT.FontWeight.FW_LIGHT };
        PdfFont font = PdfFont.CreateWindowsFont(page.Document, lf, false, false);
        return font;
    }*/

    //Available since v.3.34.2704
    private PdfFont CreateFont(PdfPage page, bool isBold, bool isItalic, string fontName)
    {
        string faceName = fontName;
        faceName += isBold || isItalic ? "," : "";
        faceName += isBold ? "Bold" : "";
        faceName += isItalic ? "Italic" : "";
        return PdfFont.CreateFont(page.Document, faceName, true, 0, 0, 0, FontCharSet.ANSI_CHARSET, false);
    }

    private void InsertImageAt(PdfPage page, float x, float y, float width, float height, Bitmap image)
    {
        PdfBitmap bitmap = new PdfBitmap(image.Width, image.Height, true);
        using (var g = Graphics.FromImage(bitmap.Image))
        {
            g.DrawImage(image, 0, 0, image.Width, image.Height);
        }
        PdfImageObject imageObject = PdfImageObject.Create(page.Document, bitmap, 0, 0);
        imageObject.Matrix = new FS_MATRIX(width * 72, 0, 0, height * 72, x * 72, y * 72);
        page.PageObjects.Add(imageObject);
    }

    private void InsertText(PdfPage page, string text, float x, float y, PdfFont font, Color color, float fontSize = 11.04f, bool isAlignRight = false)
    {
        PdfTextObject textObject = PdfTextObject.Create(text, x*72, y*72, font, fontSize);
        textObject.FillColor = color;
        page.PageObjects.Add(textObject);
        if(isAlignRight)
        {
            float w = (float)textObject.BoundingBox.Width;
            textObject.Location = new PointF(textObject.Location.X - w, textObject.Location.Y);

        }

    }

    private void InsertRect(PdfPage page, float x1, float y1, float x2, float y2, Color fillColor)
    {
        PdfPathObject pathObject = PdfPathObject.Create(FillModes.Alternate, false);
        pathObject.FillColor = fillColor;
        pathObject.Path.Add(new FS_PATHPOINTF(x1 * 72, y1 * 72, PathPointFlags.MoveTo));
        pathObject.Path.Add(new FS_PATHPOINTF(x2 * 72, y1 * 72, PathPointFlags.LineTo));
        pathObject.Path.Add(new FS_PATHPOINTF(x2 * 72, y2 * 72, PathPointFlags.LineTo));
        pathObject.Path.Add(new FS_PATHPOINTF(x1 * 72, y2 * 72, PathPointFlags.LineTo));
        pathObject.Path.Add(new FS_PATHPOINTF(x1 * 72, y1 * 72, PathPointFlags.CloseFigure| PathPointFlags.LineTo));
        page.PageObjects.Add(pathObject);
    }

Edited by user Friday, August 24, 2018 1:59:12 AM(UTC)  | Reason: Not specified

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.