I have a PDFDoc, which is created from a pdf and merged with a FDFDoc. The merging, flattening and return of a new PDF File works fine.
Now i want to add the same FreeText Annotation to every page (Is to be be added depending on existing annotations at several positions in the future).
Since setLineColor is broken (https://groups.google.com/forum/#!topic/pdfnet-sdk/Xsq2Bat1ZcY) i’m trying to achive the Fontsize and Color by creating an Appearance and setting it on the Annotation.
I have worked through the sample code for Annotations and ElementBuilder, but i cannot get the Fontsize and Font Color to work. Also my text is streched to fill the 30x30 Box, which is not what i want.
I have attached a Pdf Document which was produced by the following code.
`
public InputStream merge(InputStream document, InputStream annotations) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PDFDoc pdfDoc = null;
try {
FDFDoc fdfDoc = FDFDoc.createFromXFDF(IOUtils.toString(annotations, “UTF-8”));
pdfDoc = openDocument(document);
pdfDoc.fdfMerge(fdfDoc);
String text = “some text”;
Obj indexAppearance = createIndexAppearance(pdfDoc, text);
for (PageIterator itr = pdfDoc.getPageIterator(); itr.hasNext() {
Page page = (Page) (itr.next());
//Size 30x30, location doesn’t matter at this point.
Rect indexAnnotRect = new Rect(70, 770, 100, 800);
FreeText indexAnnot = FreeText.create(pdfDoc, indexAnnotRect);
indexAnnot.setAppearance(indexAppearance);
page.annotPushBack(indexAnnot);
}
pdfDoc.flattenAnnotations(false);
pdfDoc.save(baos, 0, null);
return new ByteArrayInputStream(baos.toByteArray());
} catch (PDFNetException | IOException e) {
throw new RuntimeException(“PDF-Document konnte nicht erstellt werden.”, e);
} finally {
closeDocument(pdfDoc);
}
}
private Obj createIndexAppearance(PDFDoc pdfDoc, String text) throws PDFNetException {
ElementBuilder builder = new ElementBuilder();
ElementWriter writer = new ElementWriter();
Element element;
writer.begin(pdfDoc);
//Font Size does not work
element = builder.createTextBegin(Font.create(pdfDoc, Font.e_times_roman), 8);
writer.writeElement(element);
element = builder.createTextRun(text);
//Setting text color does not work
element.getGState().setFillColor(new ColorPt(0,1,1));
Rect bbox = element.getBBox();
writer.writeElement(element);
element = builder.createTextEnd();
writer.writeElement(element);
Obj stm = writer.end();
builder.destroy();
writer.destroy();
stm.putRect(“BBox”, bbox.getX1(), bbox.getY1(), bbox.getX2(), bbox.getY2());
stm.putName(“Subtype”, “Text”);
return stm;
}
`
document-merge.pdf (9.21 KB)