0

Using iText PDFWrite class we render the inputFile into outputFile. As inputFile has multilayer, due to that the output PDF (outputFile) has outline of its internal layers. Actually PDFWrite merge the pdf layers while rendering it, Here we want to avoid it. We want to render the visible layers/top layers only. We use PDFWrite instead of PDFCopy because we do all matrics operations(move, rotate, scale ..etc) on inputFile.

Files:

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    There is no `PDFWrite` in iText. Maybe you are taking about `PdfWriter`. It is absolutely normal that all OCG information is gone. `PdfWriter` creates a PDF document from scratch, and the content you import consists of the content streams only. All interactive features such as annotations, but also the definition of the Optional Content Groups aren't present unless you add that information. I see that you also mention `PDFCopy` (which is actually `PdfCopy`) hence you are using iText 5, not iText 7. Free support on iText 5 is being discontinued, hence we can't help you. – Bruno Lowagie Nov 03 '17 at 11:04
  • Yes it is PdfWriter. – pankaj rai Nov 03 '17 at 11:05
  • In iText 7, does PdfWriter not merge the PDF layer ? – pankaj rai Nov 03 '17 at 11:15
  • Merging PDF layers is difficult, and it may require extra development. If you are using iText 5, there is no incentive to do that exrta development, because iText 5 is "end of life". If you are using iText 7, we might consider doing some extra development (for a fee). Hence if you want to solve this problem, your best chance is to use iText 7 and become a customer of iText Software. – Bruno Lowagie Nov 03 '17 at 11:17
  • Thank you @Bruno Lowagie.I will try and update you for iText 7. – pankaj rai Nov 03 '17 at 11:28
  • Actually I also do not want to merge the layers, In Input file there is no visible outline but output file has visible outline, Hence I believe, this is because of layer merge, input file has this outline in internal layers (non-visible layers). In actual result, input and output file should look exactly same but output file has outlines. – pankaj rai Nov 03 '17 at 11:44
  • 1
    In that case, you can stay with iText 5 and use the `OCGRemover` class to remove the layers you don't want to keep. See [IText edit or remove the layer on PDF](https://stackoverflow.com/questions/17687663/itext-edit-or-remove-the-layer-on-pdf) – Bruno Lowagie Nov 03 '17 at 12:27

1 Answers1

0

A bit late but as far as I understand the question, you want to ensure that only visible layers are rendered during processing because your file has some invisible or hidden layers which re-appear. Since iText support will not help you, there is another (free/GPL License) way.

The first step is to 'burn in' the state of the layers and remove the optional content while honoring the visibility state, using another processor. Once that step is done you can continue with whatever version of iText you want.

There are 2 processing options that can 'burn in' the visibility state of Optional Content Groups (= Layers = OCG)

Option 1 Use www.ghostscript.com with -sDEVICE=pdfwrite command line. See GS documentation for full command line

Option 2 Use Poppler's pdftocairo.exe with -pdf command line.

Both will create a PDF that has no interactive layers, with OCG removed. All iText operations will work as normal on that file.

Hakan Usakli
  • 492
  • 5
  • 11