0

When i generates a PDF file from an existing PDF file with itextsharp,my work code is

The soruce pdf

        string sourceFile = "a4.pdf", targetFile = "processed.pdf";
        PdfReader reader = new PdfReader(sourceFile);
        Document doc = new Document();
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(targetFile, FileMode.Create));
        doc.Open();
        PdfContentByte cb = writer.DirectContent;
        PdfImportedPage page;
        for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
        {
            doc.SetPageSize(reader.GetPageSizeWithRotation(pageNumber));
            doc.NewPage();
            page = writer.GetImportedPage(reader, pageNumber);
            //Write a PageIndex
            ColumnText.ShowTextAligned(cb, PdfContentByte.ALIGN_CENTER, new Phrase(pageNumber.ToString()), 100, 0, 0);
            cb.AddTemplate(page, 0, 0);
        }
        doc.Close();

The problem is, when i get a PdfImportedPage from reader,page = writer.GetImportedPage(reader, pageNumber); the content in sourceFile's hidden layer will display.The processed.pdf has none layer.

How can i get a PdfImportedPage without hidden layer context use Itextsharp.

Neeraj Kumar
  • 771
  • 2
  • 16
  • 37
zhusp
  • 131
  • 12
  • You have to use the `OCGRemover` class to do that: https://stackoverflow.com/questions/17687663/itext-edit-or-remove-the-layer-on-pdf – Bruno Lowagie Jun 15 '17 at 08:10
  • Your task appears to be to add page numbers to a PDF. If you used a `PdfStamper` for that instead of that inappropriate copying routine, the hidden layer would remain a hidden layer. – mkl Jun 15 '17 at 08:43
  • Ths a lot! but i can't find the hidden layer. – zhusp Jun 15 '17 at 09:53
  • @mkl `var canvas = stamper.GetOverContent(page);` can do it.But the canvas has the same bleedbox/cropbox/trimbox as the sourceFile.This will trouble me in other operation!is there some method to remove those box?how kind of you!ths. – zhusp Jun 15 '17 at 10:09
  • Strictly speaking that is an entirely different question, so you should ask it in an actual question, not a comment, if you want a detailed answer. That been said, yes, you can simply change those boxes. Removing them is a bad idea, at least the media box must remain for a valid page. You set the box values by retrieving the page dictionary in question and put box entries into it accordingly. Cf. [this answer](https://stackoverflow.com/a/21378162/1729265) or [this one](https://stackoverflow.com/a/32818299/1729265). – mkl Jun 15 '17 at 12:43

0 Answers0