3

I have created some annotations with PDFBox and I dont want other readers to edit/change the annotations.

I tried to use

PDAnnotation.setLocked(true);
PDAnnotation.setReadOnly(true);
PDAnnotation.setNoRotate(true);

to fixed the annotations, and works perfect.

However, the Cloudy Polygon, Free Text with Chinese will disappeared when using in-browser pdf reader (like Edge, Google Chrome).

Reading in Adobe Acrobat Reader DC https://i.stack.imgur.com/ORvmc.jpg

Reading in Google Chrome https://i.stack.imgur.com/zZbob.jpg

I know that there is a function can be flatten the PDF in PDFBox, but it only flatten the acroform fields, cannot flatten annotations. Is there any method to make sure all annotations can be showed in any readers?

b7184756
  • 45
  • 4
  • What PDFBox version are you using? Since 2.0.16 creating appearance streams is supported for many annotation types by calling `annotation.constructAppearances()` (cloudy is supported for sure, but I'm not sure about chinese text). Flatten isn't the solution there, the problem is the missing appearance stream. – Tilman Hausherr Aug 09 '19 at 08:44
  • Thank you @TilmanHausherr, I am using PDFBox 2.0.16 and tried to use annotation.constructAppearances() after read your comment. Got an exception "is not available in this font Helvetica encoding: WinAnsiEncoding", I think it is the problem of the Chinese Words – b7184756 Aug 09 '19 at 09:22
  • After using `annotation.constructAppearances()`, it is OK to show the cloudy polygon now, but I find that the Link annotation (the PDF is printed from a website) is inactived, is it functioning properly? – b7184756 Aug 09 '19 at 09:34
  • Re the link annotation, I would have to see the PDF file (sometimes things look like links, but this is just underlined text). Re the chinese text, I had a quick look into the sources, it is indeed only Helvetica at this time :-( – Tilman Hausherr Aug 09 '19 at 10:26
  • Oh...its important for our user to show the chinese text :-(, and here is my sample pdf & xfdf: [link](https://1.bitsend.jp/download/c10903041b8af47195daeef1f471a366.html) . Anyways, thanks @TilmanHausherr – b7184756 Aug 09 '19 at 10:44
  • What could be done would be to assign your own custom appearance handler with `setCustomAppearanceHandler()`, and have that one use the new font. The code would just be a modified version of `PDFreeTextAppearanceHandler.java` from the source code download. I'll investigate that when I have more time... – Tilman Hausherr Aug 09 '19 at 11:12
  • Re the link annotation, it's "our fault", it's not implemented yet. But I think it is possible, less than 1 day of work. Re the FreeText annotation, the thing I mentioned would be possible, but it would make your code more difficult (you want to handle XFDF files as in your other question). I've created a first issue for now at https://issues.apache.org/jira/browse/PDFBOX-4628 . – Tilman Hausherr Aug 09 '19 at 18:13
  • In the meantime the external links from your file are supported in the snapshot build. Re the FreeText annotation, subclassing PDFreeTextAppearanceHandler.java is a real pain, because you'd have to copy other classes. The easier way to go would be to grab the PDFBox source, and change the "PDFont font = PDType1Font.HELVETICA;" line with "PDFont font = PDType0Font.load(document, new File("c:/windows/fonts/arialuni.ttf"));" and make sure that the PDDocument object is passed to the class. Yes, it's difficult, we'll probably have to improve something :-( – Tilman Hausherr Aug 11 '19 at 08:25
  • @TilmanHausherr , thank you very much! I have successfully show the chinese text by following your advice. And then I found new issues about the font color, i think it is far away for this topic, so I created a [new thread](https://stackoverflow.com/questions/57459335/how-to-set-the-font-color-for-pdannotationfreetext-with-using-pdfbox-2-0-16) – b7184756 Aug 12 '19 at 10:16
  • We've now modified PDFreeTextAppearanceHandler.java. The PDDocument is now accessible, and the version in the repository tries to get the font from the AcroForm default resources. I don't know if this solves all your problems, but it should be a step in the right direction. – Tilman Hausherr Aug 15 '19 at 19:14
  • seems not applicable in my scenario , my documents doesnt contains AcroForm :-( Currently I'm trying to load different fonts when the ````annotation.getContents()```` contain Unicode – b7184756 Aug 16 '19 at 04:06
  • Then you'll likely still need a "special" solution until we'll come up with something. Our work is being tracked in https://issues.apache.org/jira/browse/PDFBOX-3353 – Tilman Hausherr Aug 16 '19 at 08:14

0 Answers0