Hitesh Bhatt
Hitesh Bhatt

Reputation: 63

Blank PDF generate when WriteCompatiblePdf method call of ITextSharp

Hi i am using below code. When i upload some of PDF's which contain some content and after uploading, their is no content on uploaded PDF. Uploaded PDf is blank. I am using below method of ItextSharp for change the version of Orginial PDF to Defined Version.

private int WriteCompatiblePdf(string fileName, FileUpload filePath)
        {
            string sNewPdf = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["InterfaxPath"]) + fileName;
            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(filePath.FileBytes);
            // we retrieve the total number of pages
            int n = reader.NumberOfPages;
            // step 1: creation of a document-object
            iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(sNewPdf, FileMode.Create, FileAccess.ReadWrite));
            //write pdf that pdfsharp can understand
            writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
            // step 3: we open the document
            document.Open();
            iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            iTextSharp.text.pdf.PdfImportedPage page;
            int rotation;
            int i = 0;
            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                page = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);                    
                cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);                    
            }
            // step 5: we close the document
            document.Close();
            return n;
        }

Any recommendations?

Upvotes: 0

Views: 1320

Answers (2)

mkl
mkl

Reputation: 95918

The OP provided a sample "blank" file.

As it turns out, this "blank" file does contain the desired content, it merely is way off-page.

Details

The pages of the document have this media box definition:

/MediaBox[0 7072 612 7864]

Thus, the visible area has x coordinates in 0..612 and y coordinates in 7072..7864. When adding the imported page contents, though, the OP explicitly anchors them explicitly at the coordinates 0,0:

cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);

The contents of the former page, therefore, are added in the area with y coordinates 0..792. So, they are not visible.

Resolutions

There are different ways to resolve this issue:

  1. add the contents at the position with the correct coordinates, i.e. use

    cb.AddTemplate(page, 1f, 0, 0, 1f, x, y);

    where x and y are Left and Bottom of reader.GetPageSizeWithRotation(1) (a Rectangle); or

  2. normalize the page size rectangle for the Document constructor to be based in 0,0; or

  3. use a PdfStamper with the PdfReader instead of the PdfWriter and use it to select the desired version.

Upvotes: 2

Altiano Gerung
Altiano Gerung

Reputation: 854

it's because you're not set any text to be written in the PDF. this is a simple example how to write text

 string newFile = @"C:\the path\file.pdf";
 Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);
 PdfWriter write = PdfWriter.GetInstance(doc, new FileStream(newFile,  
 FileMode.Create));    

 doc.Open(); // open the connection
 Paragraph p1 = new Paragraph("text to be displayed in the first paragraph");
doc.Add(p1); // close the connection

this link will tell you the proper way to write from iTextSharp more advanced

Upvotes: 1

Related Questions