My use case is to have a button like so on a pdf page (really to add them to existing pages but for now I just want to see it work on anything).
----------
- Back -
----------
And what it does is just closes the current pdf page. The idea is to have multiple tabs opened and each tab is a pdf and then when you hit the "Back" button it closes the current pdf which will then focus back to the previous pdf. This is what I have been trying to use so far.
// Create a new empty document
try {
PDDocument document = new PDDocument();
// Create a new blank page and add it to the document
PDPage blankPage = new PDPage();
document.addPage( blankPage );
PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
PDColor green = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE);
// PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
// textWidth = (font.getStringWidth("Click Here") / 1000) * 18;
PDAnnotationLink txtLink = new PDAnnotationLink();
txtLink.setBorderStyle(borderULine);
// add an action
// PDActionURI action = new PDActionURI();
// action.setURI("www.google.com");
PDActionJavaScript action = new PDActionJavaScript("this.closeDoc()");
txtLink.setAction(action);
txtLink.setContents("HI");
txtLink.setColor(green);
PDRectangle position = new PDRectangle();
position.setLowerLeftX(10);
position.setLowerLeftY(20);
position.setUpperRightX(100);
position.setUpperRightY(40);
txtLink.setRectangle(position);
txtLink.setInvisible(false);
blankPage.getAnnotations().add(txtLink);
// Save the newly created document
document.save("C:\\Users\\jsmith\\Desktop\\demo\\BlankPage.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
}
And I cant seem to see anything on the pdf page (its just all white), I did get the following code at at least be able to go to a new page instead of the javascript but it was still invisible. I just was able to hover over the bottom left and notice i could click on a link.
PDActionURI action = new PDActionURI();
action.setURI("www.google.com");
Improved answer as discussed in the comments of the OPs own answer, and it also includes the answer from the follow-up question.
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
COSDictionary acroFormDict = new COSDictionary();
PDAcroForm acroForm = new PDAcroForm(doc, acroFormDict);
doc.getDocumentCatalog().setAcroForm(acroForm);
acroForm.setFields(new ArrayList<>());
PDPushButton button = new PDPushButton(acroForm);
button.setPartialName("Btn1");
PDActionJavaScript actionJavaScript = new PDActionJavaScript("this.closeDoc();");
PDAnnotationAdditionalActions additionalActions = new PDAnnotationAdditionalActions();
additionalActions.setU(actionJavaScript);
// widget
PDAnnotationWidget widget = button.getWidgets().get(0);
widget.setActions(additionalActions);
widget.setRectangle(new PDRectangle(100, 700, 100, 50));
PDColor colourBlack = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE);
PDAppearanceCharacteristicsDictionary fieldAppearance
= new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance.setBorderColour(colourBlack);
widget.setAppearanceCharacteristics(fieldAppearance);
// Create appearance
PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
PDAppearanceStream appearanceStream = new PDAppearanceStream(doc);
appearanceStream.setResources(new PDResources());
try (PDPageContentStream cs = new PDPageContentStream(doc, appearanceStream))
{
PDRectangle bbox = new PDRectangle(widget.getRectangle().getWidth(), widget.getRectangle().getHeight());
appearanceStream.setBBox(bbox);
cs.setNonStrokingColor(0, 0, 0); // black
cs.addRect(bbox.getLowerLeftX() + 0.5f, bbox.getLowerLeftY() + 0.5f, bbox.getWidth() - 1, bbox.getHeight() - 1);
cs.stroke();
// put some useful text
cs.setFont(PDType1Font.HELVETICA, 20);
cs.beginText();
cs.newLineAtOffset(20, 20);
cs.showText("Close");
cs.endText();
}
appearanceDictionary.setNormalAppearance(appearanceStream);
widget.setAppearance(appearanceDictionary);
page.getAnnotations().add(widget);
acroForm.getFields().add(button);
doc.save("..../Button.pdf");
doc.close();
Related
I am working on a requirement that , I need to mask the certain content based on Keys. With the PDF Rectangle fill i am able to mask well. But the user still able to get the content from masked region by selecting the text. How can i get rid of it.
PDDocument document = new PDDocument();
for (int i=0; i<1; i++) {
PDPage blankPage = new PDPage();
document.addPage( blankPage );
PDPageContentStream contentStream = new PDPageContentStream(document, document.getPage(0));
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
contentStream.newLineAtOffset(25, 500);
String text = "This is the sample document and we are adding content to it.";
contentStream.showText(text);
contentStream.endText();
contentStream.setNonStrokingColor(Color.RED);
contentStream.addRect(25, 500, 100, 100);
contentStream.fill();
contentStream.close();
}
document.save("C:/PdfBox_Examples/my_doc.pdf");
System.out.println("PDF created");
document.close();
I've been trying to compose a pie chart using XChart then vectorising it to SVG format to use it in a PDF built with PDFBox.
To do that, I use:
XChart for the pie chart building and vectorising it ;
SVGSalamender for the Graphics2D building based on previous SVG vectorising ;
PDFBoxGraphics2D for adding previous Graphics2D to my PDF.
Everything runs fine (in compiles and runs without errors) but it does not work. The output PDF stays empty.
This is my code:
#Test
public void test() {
try {
PieChart camembertXChart = camembertXChart();
BufferedImage imageCamembertXChart = BitmapEncoder.getBufferedImage(camembertXChart);
ByteArrayOutputStream output = new ByteArrayOutputStream();
File svg = new File("F:\\Users\\mangin\\Desktop\\Camembert_XChart.svg");
VectorGraphicsEncoder.saveVectorGraphic(camembertXChart, svg.getName(), VectorGraphicsFormat.SVG);
byte[] data = output.toByteArray();
FileInputStream input = new FileInputStream(svg);
BufferedImage imageCamembertXChartVectorise = ImageIO.read(input);
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contenu = new PDPageContentStream(document, page, AppendMode.APPEND, false, true);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 200, 200);
// SVG Salamander
SVGUniverse universSVG = new SVGUniverse();
SVGDiagram diagramSVG = universSVG.getDiagram(universSVG.loadSVG(svg.toURI().toURL()));
diagramSVG.render(pdfBoxGraphics2D);
pdfBoxGraphics2D.dispose();
var xform = pdfBoxGraphics2D.getXFormObject();
var transform = AffineTransform.getTranslateInstance(200, 200);
xform.setMatrix(transform);
contenu.drawForm(xform);
contenu.close();
document.save("D://tests/archi/PDF_avec_elements_graphique.pdf");
document.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private PieChart camembertXChart() {
PieChart camembert2 = new PieChartBuilder().width(200).height(200).build();
Color[] sliceColors = new Color[] {
new Color(183, 203, 231),
new Color(131, 173, 219),
new Color(84, 143, 197),
new Color(70, 120, 165) };
camembert2.getStyler().setSeriesColors(sliceColors);
camembert2.getStyler().setLegendVisible(false);
camembert2.getStyler().setPlotBorderVisible(false);
camembert2.getStyler().setChartTitleVisible(false);
camembert2.getStyler().setChartTitleBoxVisible(false);
camembert2.getStyler().setChartPadding(0);
camembert2.getStyler().setChartTitlePadding(0);
camembert2.getStyler().setBorderWidth(3);
camembert2.getStyler().setAnnotationsFont(new Font("Arial", Font.BOLD, 10));
camembert2.getStyler().setAnnotationsFontColor(Color.WHITE);
camembert2.addSeries("Item 1", 9);
camembert2.addSeries("Item 2", 10);
camembert2.addSeries("Item 3", 23);
camembert2.addSeries("Item 4", 58);
return camembert2;
}
Does anyone have any experience in an equivalent development ? Or does anybody know why the PDF is empty?
Thanks and regards,
Thomas.
I have successfully converted an Image to Pdf. My issue is that the pdf is displaying half of the width
My Code:
#FXML
private void print() {
try {
WritableImage nodeshot = stackPane.snapshot(new SnapshotParameters(), null);
File file = new File("C:/Users/Andre Kelvin/Desktop/TheNode.png");
ImageIO.write(SwingFXUtils.fromFXImage(nodeshot, null), "png", file);
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
PDImageXObject pdimage;
PDPageContentStream content;
pdimage = PDImageXObject.createFromFile("C:/Users/Andre Kelvin/Desktop/TheNode.png", doc);
content = new PDPageContentStream(doc, page);
content.drawImage(pdimage, 0, 0);
content.close();
doc.addPage(page);
doc.save("C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
doc.close();
file.delete();
//This Line Automatically Opens the user defualt pdf file viewer
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
} catch (Exception e) {
}
}
I have tried getting the root node width and height by using this line:
content.drawImage(pdimage, 0, 0,(float)stackPane.getPrefWidth(),(float)stackPane.getPrefHeight());
and this:
content.drawImage(pdimage, 0, 0,(float)stackPane.getMaxWidth(),(float)stackPane.getMaxHeight());
it will just display a blank white page.
This is the actual image that is Converted to pdf:
And this is the pdf of the image:
Neither the preferred size properties nor their min/max counterparts allow you to reliably determine the size of a Region. Those are just indicators and the calculated values may not match. Furthermore the Region may be resized to sizes other than the preferred size. Last but not least those properties may contain special values Region.USE_PREF_SIZE(=Double.NEGATIVE_INFINITY) and Region.USE_COMPUTED_SIZE(=-1) and even do so by default.
If you need to get the size of a node, use the boundsInLocal property:
Bounds bounds = stackPane.getBoundsInLocal();
In this case it's simpler to get the size of the snapshot instead though.
Furthermore the page size of the PDPage may not be large enough to contain the whole image. You need to scale the image instead or change the page size of the PDPage.
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
This can be done platform-independent using the HostServices available via the Application instance.
Example
#Override
public void start(Stage primaryStage) {
Button button = new Button("print");
StackPane root = new StackPane(button);
button.setOnAction(evt -> {
try {
WritableImage nodeshot = root.snapshot(new SnapshotParameters(), null);
// store image in-memory
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(SwingFXUtils.fromFXImage(nodeshot, null), "png", output);
output.close();
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
PDImageXObject pdimage;
PDPageContentStream content;
pdimage = PDImageXObject.createFromByteArray(doc, output.toByteArray(), "png");
content = new PDPageContentStream(doc, page);
// fit image to media box of page
PDRectangle box = page.getMediaBox();
double factor = Math.min(box.getWidth() / nodeshot.getWidth(), box.getHeight() / nodeshot.getHeight());
float height = (float) (nodeshot.getHeight() * factor);
// beware of inverted y axis here
content.drawImage(pdimage, 0, box.getHeight() - height, (float) (nodeshot.getWidth() * factor), height);
content.close();
doc.addPage(page);
File outputFile = new File("C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
doc.save(outputFile);
doc.close();
getHostServices().showDocument(outputFile.toURI().toString());
} catch (Exception e) {
}
});
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
in my project I need 3 checkboxes which should work like radiobuttons. But radiobuttons have some drawbacks with appearance in Adobe Reader. For this reason I have to use checkboxes. There is a very interesting answer, but I don't understand how this can be done with iText:
Java iText and custom Radiobutton behaviour
Can anybody -- especially Lonzak, the author of this posting -- help me in this case. Thanks and Kind regards, Dirk
I think the solution is to fix your current code. The following example creates 3 radiobuttons and all contain a cross. It was based on itext 5 and you may have to adapt it a bit for itext 7.
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
document.open();
RadioCheckField bt = new RadioCheckField(writer, new Rectangle(261, 576, 279, 594), "radio", "value1");
bt.setCheckType(RadioCheckField.TYPE_CROSS);
bt.setBackgroundColor(BaseColor.WHITE);
//bt.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
bt.setBorderColor(BaseColor.BLACK);
bt.setTextColor(BaseColor.BLACK);
bt.setBorderWidth(BaseField.BORDER_WIDTH_THIN);
bt.setChecked(false);
PdfFormField f1 = bt.getRadioField();
bt.setOnValue("value2");
bt.setChecked(true);
bt.setBox(new Rectangle(287, 577, 305, 595));
PdfFormField f2 = bt.getRadioField();
bt.setChecked(false);
PdfFormField top = bt.getRadioGroup(true, false);
bt.setOnValue("value3");
bt.setBox(new Rectangle(314, 578, 332, 596));
PdfFormField f3 = bt.getRadioField();
top.addKid(f1);
top.addKid(f2);
top.addKid(f3);
writer.addAnnotation(top);
document.close();
Update: Here is your code with iText7 however there seems to be a bug so that the check style is not changed for radiobuttons. Maybe the itext guys can say more...
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output7.pdf"));
Document doc = new Document(pdfDoc);
PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdfDoc, "ExampleGroup", "");
radioGroup.setPage(1);
pdfDoc.addNewPage();
PdfFormField field1 = PdfFormField.createRadioButton(pdfDoc, new Rectangle(261, 576, 18, 18), radioGroup, "value1");
field1.setCheckType(PdfFormField.TYPE_CROSS);
field1.setValue("value1");
field1.regenerateField();
PdfFormField field2 = PdfFormField.createRadioButton(pdfDoc, new Rectangle(287, 577, 18, 18), radioGroup, "value2");
field2.setCheckType(PdfFormField.TYPE_CROSS);
field2.regenerateField();
PdfFormField field3 = PdfFormField.createRadioButton(pdfDoc, new Rectangle(314, 578, 18, 18), radioGroup, "value3");
field3.setCheckType(PdfFormField.TYPE_CROSS);
field3.regenerateField();
PdfAcroForm.getAcroForm(pdfDoc, true).addField(radioGroup);
doc.close();
Final solution with iText 7:
public static void main(String[] args) throws Exception {
// RadioButton with cross instead of bullet.
final String filename = "SampleRadioButton.pdf";
final float boxLength = 20;
final float crossWidth = 1;
final String[] languages = { "Dutch", "English", "French" };
try (PdfWriter writer = new PdfWriter(filename); PdfDocument pdfDoc = new PdfDocument(writer); Document doc = new Document(pdfDoc)) {
pdfDoc.addNewPage();
final PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
final PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdfDoc, "Language", languages[1]);
int count = 0;
for (final String language : languages) {
count++;
// button
final Rectangle rect = new Rectangle(40, 800 - (count * 30), boxLength, boxLength);
final PdfFormField radioButton = PdfFormField.createRadioButton(pdfDoc, rect, radioGroup, language);
final PdfDictionary radioDict = radioButton.getPdfObject();
final PdfDictionary mk = new PdfDictionary();
mk.put(PdfName.CA, new PdfString("8")); // check=4, circle=1, cross=8, diamond=u, square=n, star=H
radioDict.put(PdfName.MK, mk);
radioButton.setVisibility(PdfFormField.VISIBLE);
editAppearance(pdfDoc, radioButton, language, boxLength, crossWidth);
// text
final Paragraph para = new Paragraph(language).setFontSize(18);
doc.showTextAligned(para, 70, 800 - (count * 30), TextAlignment.LEFT);
}
form.addField(radioGroup);
}
Desktop.getDesktop().open(new File(filename));
}
private static void editAppearance(PdfDocument pdfDoc, PdfFormField radioButton, String value, float length, float crossWidth) {
final PdfStream streamOn = (PdfStream) new PdfStream().makeIndirect(pdfDoc);
final PdfCanvas canvasOn = new PdfCanvas(streamOn, new PdfResources(), pdfDoc);
final Rectangle rect = new Rectangle(0, 0, length, length);
final PdfFormXObject xObjectOn = new PdfFormXObject(rect);
canvasOn.saveState();
canvasOn.setStrokeColor(ColorConstants.BLACK).setLineWidth(crossWidth);
// bottom left to top right
canvasOn.moveTo(0, 0).lineTo(length, length).stroke();
// Top left to bottom right
canvasOn.moveTo(0, length).lineTo(length, 0).stroke();
canvasOn.restoreState();
xObjectOn.getPdfObject().getOutputStream().writeBytes(streamOn.getBytes());
final PdfWidgetAnnotation widget = radioButton.getWidgets().get(0);
widget.setNormalAppearance(new PdfDictionary());
widget.getNormalAppearanceObject().put(new PdfName(value), xObjectOn.getPdfObject());
}
}
I am using Java PDFBox version 2.0. I want to know how to add a back ground image to the pdf. I can not find any good example in the pdfbox.apache.org
Do this with each page, i.e. from 0 to doc.getNumberOfPages():
PDPage pdPage = doc.getPage(page);
InputStream oldContentStream = pdPage.getContents();
byte[] ba = IOUtils.toByteArray(oldContentStream);
oldContentStream.close();
// brings a warning because a content stream already exists
PDPageContentStream newContentStream = new PDPageContentStream(doc, pdPage, false, true);
// createFromFile is the easiest way with an image file
// if you already have the image in a BufferedImage,
// call LosslessFactory.createFromImage() instead
PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
newContentStream.saveGraphicsState();
newContentStream.drawImage(pdImage, 0, 0);
newContentStream.restoreGraphicsState();
newContentStream.close();
// append the saved existing content stream
PDPageContentStream newContentStream2 = new PDPageContentStream(doc, pdPage, true, true);
newContentStream2.appendRawCommands(ba); // deprecated... needs to be rediscussed among devs
newContentStream2.close();
There is another way to do it which is more painful IMHO, getting a iterator of PDStream objects from the page with getContentStreams(), build a List, and insert the new stream at the beginning, and reassign this PDStream list to the page with setContents(). I can add this as an alternative solution if needed.
Call PDPageContentStream.drawImage:
val document = PDDocument()
val page = PDPage()
document.addPage(page)
val contentStream = PDPageContentStream(document, page)
val imageBytes = this::class.java.getResourceAsStream("/image.jpg").readAllBytes()
val image = PDImageXObject.createFromByteArray(document, imageBytes, "background")
contentStream.drawImage(image, 0f, 0f, page.mediaBox.width, page.mediaBox.height)
contentStream.close()
page.close()
This worked best for me... (Please note the use of AppendMode.PREPEND)
InputStream is = getClass().getResourceAsStream("/yourImageFileNameWithExtenstion");
PDImageXObject pdImageXObject = PDImageXObject.createFromByteArray(document, is.readAllBytes(), "");
for (int i = 0; i < document.getNumberOfPages(); i++) {
PDPage page = document.getPage(i);
PDPageContentStream cos = new PDPageContentStream(document, page, AppendMode.PREPEND, true);
cos.drawImage(pdImageXObject, 0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
cos.close();
}