5

H All,

I am Using IText for adding text layers on PDF. Now i want to edit the existing layers on the PDF, layers are also created by IText only. Seems IText dont have such methods to suppport.

I thought of other way around is remove existing Layer and place new layer on its place. Seems remove also not supported by IText. Any way todo these?

Thanks in adavance.

Tobias Kienzler
  • 21,611
  • 21
  • 111
  • 204
JAVAC
  • 1,062
  • 2
  • 15
  • 32
  • 1
    PDF does not as such have a concept of layers. If you add something to a PDF in the background, you simply prepend it before the existing content; for the foreground, you append it thereafter. This been said, if you really only used iText to do such kind of changes, these change sets can be recognized and removed. It merely requires using the iText low level API. – mkl Jul 17 '13 at 07:17
  • Or do you refer to what iText calls a `PdfLayer` which actually is an optional content group in PDF lingo? – mkl Jul 17 '13 at 10:57
  • Yes i am talking about PdfLayer -- using this i will create a text and place it on PDF, we can see on PDF Viewer tools it as a layer – JAVAC Jul 17 '13 at 18:26

1 Answers1

5

As it turned out in comments, the layers in question are what iText indeed calls layers but what actually in PDF lingo is called optional content groups.

There indeed is a utility class for removing such layers in the iText Xtra package (not the extrajars, but itext-xtra.jar): com.itextpdf.text.pdf.ocg.OCGRemover which makes use of the class OCGParser in the same package.

/**
 * Class that knows how to remove OCG layers.
 */
public class OCGRemover
{
    /**
     * Removes layers from a PDF document
     * @param reader    a PdfReader containing a PDF document
     * @param layers    a sequence of names of OCG layers
     * @throws IOException
     */
    public void removeLayers(PdfReader reader, String... layers) throws IOException
    [...]
}

After applying that method to a PdfReader you obviously have to save the changes, e.g. by means of a PdfStamper.

Tobias Kienzler
  • 21,611
  • 21
  • 111
  • 204
mkl
  • 77,874
  • 12
  • 103
  • 212
  • Yes i tried this but throwing exception java.lang.NullPointerException at com.itextpdf.text.pdf.PdfReader.getStreamBytes(PdfReader.java:2281) at com.itextpdf.text.pdf.ocg.OCGParser.parse(OCGParser.java:132) at com.itextpdf.text.pdf.ocg.OCGRemover.parse(OCGRemover.java:222) at com.itextpdf.text.pdf.ocg.OCGRemover.removeLayers(OCGRemover.java:81 – JAVAC Jul 19 '13 at 01:03
  • Can you provide a sample PDF for reproducing the issue? – mkl Jul 19 '13 at 05:13
  • Ah, I just spotted one possible reason for the NPE: `OCGRemover.parse` uses `page.getAsStream(PdfName.CONTENTS)` to retrieve the page contents. But the contents do not need to be a single stream, they may also be an array of streams, and in that case `getAsStream` returns `null` which then is forwarded to `OCGParser.parse` to parse which then explodes in your face during `PdfReader.getStreamBytes.` Ok, so `OCGParser.parse` should be extended to also handle contents arrays... oh well, the xtras have a somewhat experimental character... – mkl Jul 19 '13 at 13:59
  • Might be you are true.. this xtra doesn't help me. – JAVAC Jul 21 '13 at 19:52
  • If you extend it to cope with arrays, too, it does help. If you supplied the PDF, the possible reason could be verified. – mkl Jul 21 '13 at 20:36
  • I think this was fixed either in the latest version or in the version that will be released next week (I remember encountering that problem myself and fixing it, but I don't remember when I fixed it). – Bruno Lowagie Jul 23 '13 at 11:52