Patagames

Advanced PDF editing library for total control over your PDF creation workflow

We are offering a high level c# API for PDF creation and editing. From simple splitting of documents to precise removal of a single element.

  • Create PDF document from scratch
  • Edit existing PDF documents
  • Supports the creation, insertion or removal of PDF page objects
  • Automate your PDF form filling procedures

The c# code below demonstrates the five steps required to generate the following PDF document on the fly.

public void CreateDocument()
{
    // The PDF coordinate system origin is at the bottom left corner of the page. 
    // The X-axis is pointing to the right. The Y-axis is pointing in upward direction.
	// The sizes and coordinates in this method are given in the inches.

    // Step 1: Create empty document
    // Return value: PdfDocument main class
	var doc = PdfDocument.CreateNew();

    // Step 2: Add new page
    // Arguments: page width: 8.27", page height: 11.69", Unit of measure: inches
    // Return value: PdfPage class
	var page = AddNewPage(doc, 8.27f, 11.69f);

    // Step 3: Create fonts used for text objects
    // Arguments: isBold: true/false, isItalic: true/false, font name: "Calibri"
    // Return value: PdfFont class
	PdfFont calibryBold = CreateFont(page, true, false, "Calibri");
	PdfFont calibryItalic = CreateFont(page, false, true, "Calibri");
	PdfFont calibry = CreateFont(page, false, false, "Calibri");

    // Step 4: Add graphics and text contents to the page
	// Insert logo from file using standart System.Drawing.Bitmap class
	using (Bitmap logo = Bitmap.FromFile(@"d:\0\logo_square.png") as Bitmap)
	{
		InsertImageAt(page, 0.5f, 10.07f, 1.12f, 1.12f, logo);
	}

    // Insert text objects
    // Arguments: PDF page, еext to be inserted, text position, font object, text color, font size, and a flag indicating that the text should be right-aligned
    // Return value: PdfPage class
	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);
	
    // Draw 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);

    // Insert table contents
	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);
	}
                          
    // Step 5: Generate page content and save pdf file
    // argument: PDF file name
	page.GenerateContent();
	doc.Save(@"c:\test_quote.pdf", SaveFlags.NoIncremental);
}

The code contains some helper functions (CreateFont, InsertText, etc) which are listed here:

private PdfPage AddNewPage(PdfDocument doc, float Width, float Height)
{
	//The PDF unit of measure is point. There are 72 points in one inch.
	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)
{
	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);
}                   
                   

Pdfium.Net SDK

Download

NuGet package is also available in the official repo at nuget.org

PM> Install-Package Pdfium.Net.SDK
Create PDF in c#
Create PDF in Vb.Net
Native WinForms Control
Native WPF UIElement