1

I am using PDFsharp to read / write PDF in our application. I am trying to get the paper size of a page to show in metadata window. I am using following code to do the same:

      // read PDF document into memory
      var pdfDocument = PdfReader.Open(pdfPath);

      // if PDF document has more than one page, then try to get the page size from first page
      if (pdfDocument.Pages.Count > 0)
      {
        var pageSize = pdfDocument.Pages[0].Size.ToString();
      } // if

But every time it returns 'Undefined', I even tried to create A4 page document for MS Word. But still it returns 'Undefined'.

I also created a PDF from HTML but then also page size comes as undefined.

static void Main(string[] args)
    {
      // create PDF config to generate PDF
      var config = new PdfGenerateConfig()
                   {
                     PageSize = PageSize.Letter,
                     PageOrientation = PageOrientation.Landscape
                   };

      // load the HTML file
      var html = System.IO.File.ReadAllText(@"C:\Users\mohit\Desktop\Temp\Ekta\Test.html");

      // generate the PDF
      var pdf = PdfGenerator.GeneratePdf(html,
                                         PageSize.Letter);

      // get the paper size of first page
      var size = pdf.Pages[0].Size;

      // save the pdf document
      pdf.Save(@"C:\Users\mohit\Desktop\Temp\Ekta\A13.pdf");

      Console.ReadKey();
    }
Mohit Vashistha
  • 1,824
  • 3
  • 22
  • 49
  • Here is another question from SO that might help you out: http://stackoverflow.com/questions/2370427/itextsharp-set-document-landscape-horizontal-a4 – Glenn Ferrie Oct 12 '16 at 15:08

1 Answers1

0

The properties you have to use are Width and Height. Convert them to millimeter or inch if you do not want to show the size in points.

The property Size is a convenient way to set standard page sizes for new pages as you can specify A4 or Letter. It will not be set for imported PDF files.