Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom page size in iTextSharp in C#.NET

Tags:

c#

pdf

itextsharp

I want to create a custom page size which is (5"X2") PDF using iTextSharp in C#. Is there any way to do this?

Document doc = new Document(iTextSharp.text.PageSize.A4, 15, 15, 0, 0);
like image 978
senps Avatar asked Jun 13 '13 04:06

senps


2 Answers

The document also accept the rectangle type, so we can create a rectangle of our custom size like below.

var pgSize = new iTextSharp.text.Rectangle(1000, 1000);
Document document = new Document(pgSize, 5f, 5f, 20f, 20f);
like image 194
sushil suthar Avatar answered Oct 15 '22 00:10

sushil suthar


Below code will demonstrate how to implement the custom PDF using PDF coordinates in C#.net. For this task you must aware about the pdf coordinates.

  BaseFont f_cn;
  string  poath = Server.MapPath("~/fonts/CALIBRI.TTF");

 f_cn = BaseFont.CreateFont(poath, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

using (System.IO.FileStream fs = new FileStream(Server.MapPath("~/TempPdf") + "\\" + "download.pdf", FileMode.Create))
            {
 Document document = new Document(PageSize.A4, 25, 25, 30, 30);
                PdfWriter writer = PdfWriter.GetInstance(document, fs);                
                Paragraph p = new Paragraph();
                // Add meta information to the document
                document.AddAuthor("Mikael Blomquist");
                document.AddCreator("Sample application using iTestSharp");
                document.AddKeywords("PDF tutorial education");
                document.AddSubject("Document subject - Describing the steps creating a PDF document");
                document.AddTitle("The document title - Amplified Resource Group");
                // Open the document to enable you to write to the document
                document.Open();
                // Makes it possible to add text to a specific place in the document using 
                // a X & Y placement syntax.
                PdfContentByte cb = writer.DirectContent;
                cb.SetFontAndSize(f_cb, 16);
                // First we must activate writing
                cb.BeginText();
                // Add an image to a fixed position from disk
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/arg.png"));
                png.SetAbsolutePosition(200, 738);
                cb.AddImage(png);
                writeText(cb, "Header", 30, 718, f_cb, 14);
}
 private void writeText(PdfContentByte cb, string Text, int X, int Y, BaseFont font, int Size)
    {
        cb.SetFontAndSize(font, Size);
        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, Text, X, Y, 0);
  }
like image 3
Anto Avatar answered Oct 15 '22 01:10

Anto