Search code examples
itext7pdfsharp

PdfSharp and iText7


I am replacing PdfSharp with iText7 and I am not sure why when I use the same x and y coordinates I am getting different results. I am using 735 and 520 on both but they are printing on different location on the pdf file. Any help with this would be great. PdfSharp is using a double and iText7 is using float but they are exactly the same, under the hood.

the origin (0, 0) is top left and coordinates grow right and down. The unit of measure is always point (1/72 inch). http://www.pdfsharp.net/wiki/Graphics.ashx

PdfSharp

 PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
 dfSharp.Drawing.Layout.XTextFormatter tf = new XTextFormatter(gfx);
 pnt = new XPoint(735, 520);
 gfx.DrawString("Text Enter", font, XBrushes.White, pnt);

iText7

  iText.Kernel.Geom.Rectangle rectangle = new iText.Kernel.Geom.Rectangle(735, 520, 100, 100);
  Canvas canvas = new Canvas(pdfCanvas, rectangle);
  Style normal = new Style();
  PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
                            normal.SetFont(font).SetFontSize(34).SetFontColor(ColorConstants.WHITE);


  Paragraph p = new Paragraph()
  .Add(_versionNumber)
  .SetFontSize(34)
  .SetFontColor(ColorConstants.WHITE)
  .SetFont(font);

Solution

  • paragraph.SetFixedPosition(0, 800, 200) will put you at the Top left edge of the page, 200 is the width. Itext7 PDF coordinates start at the bottom left. (0,0) is the bottom left corner of the document normally.