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 2:35:12 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 7 times
Was thanked: 128 time(s) in 125 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 16 time(s).
quotation.png (49kb) downloaded 53 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);

    //Insert logo from file
    using (Bitmap logo = Bitmap.FromFile(@"e:\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, CreateCalibryBold(doc), FS_COLOR.Black, 11.04f, true);
    InsertText(page, "sales@patagames.com", 7.69f, 10.83f, CreateCalibryBold(doc), FS_COLOR.SteelBlue, 11.04f, true);
    InsertText(page, "https://patagames.com", 7.69f, 10.65f, CreateCalibryBold(doc), FS_COLOR.SteelBlue, 11.04f, true);
    InsertText(page, DateTime.Now.ToString("MMM. dd, yyyy"), 7.69f, 10.30f, CreateCalibryBold(doc), FS_COLOR.Black, 11.04f, true);
    InsertText(page, "#1804051", 7.69f, 10.11f, CreateCalibryBold(doc), FS_COLOR.Black, 11.04f, true);
    InsertText(page, "Quotation", 3.38f, 9.37f, CreateCalibryBold(doc), FS_COLOR.Black, 26.04f);
    InsertText(page, "Dear %username%,", 0.51f, 9.02f, CreateCalibry(doc), FS_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, CreateCalibry(doc), FS_COLOR.Black);
    InsertText(page, "Details", 0.51f, 7.97f, CreateCalibryItalic(doc), FS_COLOR.Black, 12.96f);
    InsertText(page, "Quantity", 4.96f, 7.97f, CreateCalibryItalic(doc), FS_COLOR.Black, 12.96f);
    InsertText(page, "Price", 6.20f, 7.97f, CreateCalibryItalic(doc), FS_COLOR.Black, 12.96f);
    InsertText(page, "Amount", 6.86f, 7.97f, CreateCalibryItalic(doc), FS_COLOR.Black, 12.96f);

    //draw line at the top edge
    InsertRect(page, 0.41f, 10.0600f, 7.69f, 10.0601f, FS_COLOR.Black);
    //draw table background
    InsertRect(page, 4.88f, 7.47f, 7.67f, 7.91f, FS_COLOR.WhiteSmoke);
    InsertRect(page, 4.88f, 7.03f, 7.67f, 7.25f, FS_COLOR.WhiteSmoke);
    //draw table lines
    InsertRect(page, 0.43f, 7.9100f, 7.67f, 7.9101f, FS_COLOR.Black);
    InsertRect(page, 4.8700f, 7.91f, 4.8801f, 7.03f, FS_COLOR.Black);

    InsertText(page, "Pdfium.Net SDK", 4.8f, 7.49f, CreateCalibryItalic(doc), FS_COLOR.Black, 12.96f, true);
    InsertText(page, "Tesseract.Net SDK", 4.8f, 7.27f, CreateCalibryItalic(doc), FS_COLOR.Black, 12.96f, true);
    InsertText(page, "15", 5.44f, 7.51f, CreateCalibry(doc), FS_COLOR.Black, 11.04f, true);
    InsertText(page, "1", 5.44f, 7.29f, CreateCalibry(doc), FS_COLOR.Black, 11.04f, true);

    InsertText(page, "400 USD", 6.52f, 7.51f, CreateCalibry(doc), FS_COLOR.Black, 11.04f, true);
    InsertText(page, "450 USD", 6.52f, 7.29f, CreateCalibry(doc), FS_COLOR.Black, 11.04f, true);
    InsertText(page, "Total", 6.52f, 7.11f, CreateCalibryBold(doc), FS_COLOR.Black, 11.04f, true);

    InsertText(page, "6000 USD", 7.6f, 7.51f, CreateCalibry(doc), FS_COLOR.Black, 11.04f, true);
    InsertText(page, "450 USD", 7.6f, 7.29f, CreateCalibry(doc), FS_COLOR.Black, 11.04f, true);
    InsertText(page, "6450 USD", 7.6f, 7.11f, CreateCalibryBold(doc), FS_COLOR.Black, 11.04f, true);

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

    InsertText(page, "Please visit the following link for ordering", 0.51f, 5.35f, CreateCalibry(doc), FS_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, CreateCalibry(doc), FS_COLOR.SteelBlue);

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


    page.GenerateContent();

    doc.Save(@"e:\10\test.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 void InsertText(PdfPage page, string text, float x, float y, PdfFont font, FS_COLOR FS_COLOR, float fontSize = 11.04f, bool isAlignRight = false)
{
    PdfTextObject textObject = PdfTextObject.Create(text, x * 72, y * 72, font, fontSize);
    textObject.FillColor = FS_COLOR;
    page.PageObjects.Add(textObject);
    if (isAlignRight)
    {
        float w = textObject.BoundingBox.Width;
        textObject.Location = new FS_POINTF(textObject.Location.X - w, textObject.Location.Y);

    }
}

private void InsertRect(PdfPage page, float x1, float y1, float x2, float y2, FS_COLOR fillFS_COLOR)
{
    PdfPathObject pathObject = PdfPathObject.Create(FillModes.Alternate, false);
    pathObject.FillColor = fillFS_COLOR;
    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);
}

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 PdfFont CreateCalibryBold(PdfDocument doc)
{
    return PdfFont.CreateFont(doc, "Calibri", FontCharSet.DEFAULT_CHARSET, true);
}

private PdfFont CreateCalibryItalic(PdfDocument doc)
{
    return PdfFont.CreateFont(doc, "Calibri", FontCharSet.DEFAULT_CHARSET, false, true);
}

private PdfFont CreateCalibry(PdfDocument doc)
{
    return PdfFont.CreateFont(doc, "Calibri");
}

Edited by user Monday, July 15, 2019 1:23:52 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.