I am trying to highlight some text and convert it to image.
I tried some stuff but the annotation did not came out on the image.
Looking for help found this issue
http://issues.apache.org/jira/browse/PDFBOX-2162
which said that I must set appearance-stream to the annotation, something that acrobat reader do it automatically, but when converting to image it is needed. I could not figure out how to set the appearance-stream to the annotation.
looked for some examples on annotations and appearance-stream
(which I could not find an example that do both.. :-( )
This is what I have so far:
PDDocument document = PDDocument.load("sometest.pdf");
List<PDPage> pages = document.getDocumentCatalog().getAllPages();
PDPage page = pages.get(0);
List<PDAnnotation> annotations = page.getAnnotations();
PDAnnotationTextMarkup annotation = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
final PDRectangle boundingBox = new PDRectangle();
//here I set the boundingbox coordinates
annotation.setRectangle(boundingBox);
final float[] quads = this.getQuads(boundingBox);
annotation.setQuadPoints(quads);
annotation.setContents("bla bla");
annotation.setConstantOpacity((float) 0.9);
PDGamma c = new PDGamma();
//Here I set the RGB
annotation.setColour(c);
annotation.setPrinted(true);
//create the Form for the appearance stream
PDResources holderFormResources = new PDResources();
PDStream holderFormStream = new PDStream(document);
PDXObjectForm holderForm = new PDXObjectForm(holderFormStream);
holderForm.setResources(holderFormResources);
holderForm.setBBox(boundingBox);
holderForm.setFormType(1);
// trying to set the appreanceStream for the annotation
PDAppearanceDictionary appearance = new PDAppearanceDictionary();
appearance.getCOSObject().setDirect(true);
PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForm.getCOSStream());
holderForm.getCOSStream().createFilteredStream();
appearance.setNormalAppearance(appearanceStream);
annotation.setAppearance(appearance);
annotations.add(annotation);
//convert to image
BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_BGR, 600);
ImageIO.write(image, "jpg", new File("test.jpg"));
document.save("test.pdf");
document.close();
The "test.pdf" come out with the annotation, but the image not.
What am I missing?
Related
I tried to use the following code to convert the stamp color, but it didn't work
PDFDocumentSignature signature = signatures.get(i);
PDPageContentStream contents2 = new PDPageContentStream(pdDocument, pages,PDPageContentStream.AppendMode.APPEND, false, false);
PDExtendedGraphicsState r01 = new PDExtendedGraphicsState();
r01.setBlendMode(BlendMode.SATURATION);
contents2.setGraphicsStateParameters(r01);
contents2.setNonStrokingColor(Color.DARK_GRAY);
contents2.addRect(signature.getX(), signature.getY(), signature.getHeight(), signature.getHeight());
contents2.fill();
contents2.close();`
As it turned out in the comments to the question, the red stamp in question is a signature form field widget.
This explains why the OP's code could not de-saturate the stamp: That code de-saturates a portion of the static page content. Widgets (and annotations in general), though, are not part of the static page content but are rendered on top of it.
Thus, we have to manipulate the content stream of the widget annotation in question or another annotation over it.
You can manipulate the signature appearance like this:
PDDocument pdf = ...;
PDAcroForm acroForm = pdf.getDocumentCatalog().getAcroForm();
PDTerminalField acroField = (PDTerminalField) acroForm.getField("Signature1");
PDAnnotationWidget widget = acroField.getWidgets().get(0);
PDAppearanceStream appearance = widget.getAppearance().getNormalAppearance().getAppearanceStream();
byte[] originalBytes;
try ( InputStream oldContent = appearance.getContents() ) {
originalBytes = IOUtils.toByteArray(oldContent);
}
try ( PDPageContentStream canvas = new PDPageContentStream(pdf, appearance) ) {
canvas.appendRawCommands(originalBytes);
PDExtendedGraphicsState r01 = new PDExtendedGraphicsState();
r01.setBlendMode(BlendMode.SATURATION);
canvas.setGraphicsStateParameters(r01);
canvas.setNonStrokingColor(Color.DARK_GRAY);
PDRectangle bbox = appearance.getBBox();
canvas.addRect(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getWidth(), bbox.getHeight());
canvas.fill();
}
pdf.save(RESULT);
(ChangeAppearance test testRemoveAppearanceSaturation)
before
after
I want to create a pdf document using iTextpdf 7 but I don't want to use any of the default page sizes.
I want to set the width and height of my paper size but when I try to do it using Rectangle() class, it shows me errors and I can not create anything.
I've never used this library before and I don't know how to do it well.
This is an example of the code I made:
String url_file= "C:\\Users\\Mike89\\Documents\\PDFJava\\pdfFiles\\SALES\\SALEINVOICE"+id+".pdf";
PdfWriter writer = new PdfWriter(url_file);
PdfDocument pdf = new PdfDocument(writer);
Rectangle pagesize = new Rectangle(148, 350);
Document document = new Document(pdf, pagesize);
document.setMargins(2, 2, 2, 2);
Table table1 = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth().setBorder(Border.NO_BORDER);
Cell cell1;
cell1 = new Cell();
cell1.add(new Paragraph("COMPANY NAME").setTextAlignment(TextAlignment.CENTER).setFontSize(5).setBold()).setBorder(Border.NO_BORDER);
cell1.add(new Paragraph("DOCUMENT ID").setTextAlignment(TextAlignment.CENTER).setFontSize(5)).setBorder(Border.NO_BORDER);
cell1.add(new Paragraph("COMPANY ADDRESS").setTextAlignment(TextAlignment.CENTER).setFontSize(5)).setBorder(Border.NO_BORDER);
cell1.add(new Paragraph("TELEPHONE").setTextAlignment(TextAlignment.CENTER).setFontSize(5)).setBorder(Border.NO_BORDER);
table1.addCell(cell1);
document.add(table1);
document.close();
The error that netbeans shows me this:
incompatible types: Rectangle cannot be converted to PageSize
I don't want to use PageSize.A8 or A9 or A10 or anything like that. I just want to create my own page size, but I can't figure out what's wrong with my code. What can I do to solve this?
Rectangle and PageSize are different types, although PageSize extends Rectangle, but the Document constructor expects a PageSize. In Netbeans, use CTRL-SPACE to see more while working:
Please replace this
Rectangle pagesize = new Rectangle(148, 350);
Document document = new Document(pdf, pagesize);
with this:
PageSize pagesize = new PageSize(148, 350);
Document document = new Document(pdf, pagesize);
PDFBox offer functions to render a entire page, but no way to render only a specific rectangle of the page.
This code is working for me. But as mentioned above it renders the whole page and I need a method to render a little area of the pdf file:
File file = new File("package.pdf");
PDDocument document = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(document);
BufferedImage image = renderer.renderImageWithDPI(0, 400);
ImageIO.write(image, "PNG", new File("C:/package1.png"));
document.close();
I would be very happy about a solution, since I have not found a solution for hours
I have found a soulution by myself. CropBox was the deciding keyword i did not know about.
File file = new File("package.pdf");
PDDocument document = PDDocument.load(file);
PDPage page = document.getPage(0);
page.setCropBox(new PDRectangle(133f, 150f, 100f, 100f)); // Here you draw a rectangle around the area you want to specify
PDFRenderer renderer = new PDFRenderer(document);
BufferedImage image = renderer.renderImageWithDPI(0, 400);
ImageIO.write(image, "PNG", new File("C:/fatihabi.png"));
document.close();
I'm using PDFBox in my application to modify existing PDF files.
I need to place a barcode to the first page of the PDF document.
So first i create new PDF file with inserted barcode:
PDDocument document = new PDDocument();
PDXObjectImage ximage = new PDPixelMap(document, awtImage);
PDPage pag = new PDPage(new PDRectangle(ximage.getWidth(), ximage.getHeight()));
document.addPage(pag);
PDPageContentStream stream = new PDPageContentStream(document, pag, false, false);
stream.drawXObject(ximage, 0, 0, ximage.getWidth(), ximage.getHeight());
stream.close();
Then i try to place it as new layer into existing PDF:
Point barlocation = new Point(0,0);
float Height = page.getMediaBox().getHeight();
float Width = page.getMediaBox().getWidth();
barlocation.setLocation(Width - pag.getMediaBox().getWidth(), Height - pag.getMediaBox().getHeight()); //I need to place it in the top right corner
LayerUtility layerUtility = new LayerUtility(doc);
List bigPages = doc.getDocumentCatalog().getAllPages();
PDXObjectForm firstForm = layerUtility.importPageAsForm(document, 0);
AffineTransform affineTransform = new AffineTransform();
affineTransform.translate(barlocation.x , barlocation.y);
layerUtility.appendFormAsLayer((PDPage) bigPages.get(0), firstForm, affineTransform, "barcode layer");
doc.save(path);
doc.close();
So it works well with PDF document ver 1.5 (created in MS Word 2010/2013). But it's not working correctly with PDF document version 1.6 position and size of the inserted layer are wrong (i can't post an image, so you can see the result here:http://img4.imagetitan.com/img.php?image=11_capture567.png
What should I change? Thanks for reading and sorry for my bad English.
Trying to use pdfbox to create pdf with form fields that can be filled in by user or computer.
So far my code looks like this:
PDDocument doc = new PDDDocument();
PDPage page = new PDPage();
doc.addPage(page)
PDAcroForm = new PDAcroForm(doc)
doc.documentCatalog.setAcroForm(acroForm)
COSDictionary cosDict = new COSDictionary()
PDTextbox textField = new PDTextbox(acroForm, cosDict)
PDRectangle rect = new PDRectangle()
rect.setLowerLeftX((float) 250)
rect.setLowerLeftY((float) 125)
rect.setUpperRightX((float) 500)
rect.setUpperRightY((float) 75)
textField.getWidget().setRectangle(rect)
acroForm.getFields.add(textField)
page.getAnnotations().add(textField)
page.getAnnotations().add(textField.getWidget())
I had almost the same problem. I tried to add Fields to a existing document that includes a form. I came up with the following solution:
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDAcroForm acroForm = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(acroForm);
COSDictionary cosDict = new COSDictionary();
COSArray rect = new COSArray();
rect.add(new COSFloat(250f)); // lower x boundary
rect.add(new COSFloat(75f)); // lower y boundary
rect.add(new COSFloat(500f)); // upper x boundary
rect.add(new COSFloat(125f)); // upper y boundary
cosDict.setItem(COSName.RECT, rect);
cosDict.setItem(COSName.FT, COSName.getPDFName("Tx")); // Field Type
cosDict.setItem(COSName.TYPE, COSName.ANNOT);
cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
cosDict.setItem(COSName.T, new COSString("yourFieldName"));
PDTextbox textField = new PDTextbox(acroForm, cosDict);
acroForm.getFields().add(textField);
page.getAnnotations().add(textField.getWidget());
I think the problem is that the widget doesn't write to the dictionary of the textField.