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.

3
  • 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. Commented Jul 17, 2013 at 7:17
  • Or do you refer to what iText calls a PdfLayer which actually is an optional content group in PDF lingo? Commented Jul 17, 2013 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 Commented Jul 17, 2013 at 18:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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
Can you provide a sample PDF for reproducing the issue?
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...
Might be you are true.. this xtra doesn't help me.
If you extend it to cope with arrays, too, it does help. If you supplied the PDF, the possible reason could be verified.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.