0

I am able to add a layer(optional content group) to a pdf using LayerUtility class in pdfbox library. But, I am not able to find any way to add annotations to an existing layer. I tried to convert the PDOptionalContentGroup to PDPage by first converting PDOptionalContentGroup to COSDictionary and then creating the PDPage from the COSDictionary. Then making the changes in the PDPage.But this creates an invalid PDPage. So, how do I add markups or annotation to an existing layer in pdf?

V K
  • 1,645
  • 3
  • 26
  • 57
  • @TilmanHausherr Can we also not put some text on an existing layer? – V K Apr 10 '17 at 13:24
  • Ignore my first comment (deleted), sorry. /OC entry of annotation dictionary might be what you need. There is no method, so you have to use annotation.getCOSObject().setItem(COSNAME.OC, dict); – Tilman Hausherr Apr 10 '17 at 13:26
  • @TilmanHausherr I didn't find any entry of COSObject of type OC in the PDPage.getAnnotation() list and also didn't find it in PDPage's COSDictionary. – V K Apr 10 '17 at 14:01
  • You need to call getCOSObject() on the individual annotation. – Tilman Hausherr Apr 10 '17 at 14:03
  • @TilmanHausherr For which annotation shall I set the OC key, and what does the dict contains? – V K Apr 10 '17 at 14:21
  • For the annotation that you want to belong to an OCG. Re what the dict contains - an OCG, i.e. an PDOptionalContentGroup cos object. You wrote elsewhere that you got that working. – Tilman Hausherr Apr 10 '17 at 14:26
  • @TilmanHausherr Is there a way that I can also draw some curves using PDPageContentStream on a OCG. – V K Apr 10 '17 at 14:33
  • That is a different question. You can draw whatever you want. Just put it between `beginMarkedContent()` and `endMarkedContent()`. See the source code of `layerutility.appendFormAsLayer`, and the source code of `TestLayerUtility`. – Tilman Hausherr Apr 10 '17 at 14:39
  • @TilmanHausherr Your advice worked for both annotations and contetstream, thanks .But I am using 1.8 version of pdfbox, so for the contentstream.beginMarkedContentSequence command, I am using "MC1" as resource name of OCG. Is this a safe way of creating markups on OCG? – V K Apr 11 '17 at 08:59
  • The name doesn't matter. But get away from 1.8. – Tilman Hausherr Apr 11 '17 at 09:07
  • @TilmanHausherr The 2.0.5 version of pdfbox is not available for .net, that is why I am using the older version. – V K Apr 11 '17 at 09:45
  • @TilmanHausherr Also,using MC0 , I am able to get the first layer, but I am not able to get the second layer using MC1 as recource name. – V K Apr 11 '17 at 10:02
  • I can't comment on this (not even sure whether this is a question or a remark) without having the PDF. – Tilman Hausherr Apr 11 '17 at 10:06
  • @TilmanHausherr I will create a new question for this. – V K Apr 11 '17 at 10:19

0 Answers0