JasperReports: add List of images to PDF document - java

I am generating PDF documents. It is OK. All works.
I have only one problem: on each PDF page I need about 10 images (QR codes). Is it possible to
put List of java.awt.Image to JasperReport template? Put each image in separate field is complicated...
Example:
In template I have image components with these settings (http://goo.gl/wcESGp):
Expression Class: java.awt.Image
Image Expression :
com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(
new com.google.zxing.qrcode.QRCodeWriter().encode(
$F{CONTENT_TO_ENCODE},
com.google.zxing.BarcodeFormat.QR_CODE, 300, 300))
One image is no problem - just set field in template and put image to field in Java code. How can I fill images in JasperReports report template with List<Image>?

Related

iTextPDF 7 - Table of content with clickable page numbers

I'm trying to create a table of content with iTextPDF 7 in Java from existing PDFs.
I've tried to use Link link = new Link() and PdfAction.createGoTo(PdfExplicitDestination.createFit(pageNum)) but to no avail.
I can't link the page number/row in TOC to the respective page in the end PDF (after merging).
The documentation on their website only gets me so far and I'm out of ideas.
What is the correct way to create a TOC for existing PDFs and how to implement it correctly?

Navigate to page when page number is clicked in table of contents of a pdf generated using itext

I'm using IText to generate a pdf for my project. Using help from this
How to generate a Table of Contents "TOC" with iText?, I was able to generate a pdf with TOC. However I'm unable to figure out how can I navigate to a page when page number is clicked. I tried adding action inside the pdf template but that doesn't seem to work. Thanks in advance for your help.
This is what I've added to the answer already given in the linked question:
final PdfTemplate template2 = this.tocPlaceholder.get("Test");
template2.beginText();
template2.setFontAndSize(baseFont, 12);
template2.setTextMatrix(50 - this.baseFont.getWidthPoint(String.valueOf(writer.getPageNumber()), 12), 0);
template2.showText(String.valueOf(writer.getPageNumber()));
PdfAction action = PdfAction.gotoLocalPage(writer.getPageNumber(),new PdfDestination(0),writer);
template2.setAction(action , 0,0,0,0);
template2.endText();

PDF is getting change after loading using PDFBOX jar

I have a PDF having first page as different page ( as we have in MS word functionality under "Design" tab ). and the same PDF is passed to PDFBOX using below code :
File originalPdfFile = new File("D:\\AsposeOutput_temp.pdf");
PDDocument originalDocument = PDDocument.load(originalPdfFile);
originalDocument.save("D:\\pdfBoxGen.pdf");
But when i am opening the PDF that is generated by PDFBOX, is modified. I have attached the input PDF (named AsposeOutput_temp.pdf) and output PDF (named : pdfBoxGen.pdf). I want the PDF to same as i am passing as input.
File links : https://gofile.io/?c=lLPpQz
Any help would be greatly appreciated!!
I got the solution for the above problem. There was no issue with PDFBOX library. it was with the Aspose word.The input file that was passed to PDFBOX library , was having section break internally and the same was making the improper alignment of footer.

Getting TIFF Tags from an Image

I am trying to render a Tiff Image for extracting tags from it, here is what i have done till now:
ByteArraySeekableStream sStream= new ByteArraySeekableStream(imageByteArray);
ImageDecoder imgDecoder = ImageCodec.createImageDecoder("TIFF",sStream,null);
RenderedImage renderedImage = imgDecoder.decodeAsRenderedImage();
this seems to work fine for all positive scenarios, where as suppose if we have a TIFF image which has a missing tag in it.
for eg: If Image Length is missing, then i get the following exception:
java.io.IOException: "Image Length", a required field, is not present in the TIFF file
but i do not want it to throw any exception, rather it should render the image with whatever TIFF tags are present.
Is there any other way of rendering a TIFF image? or is there a way of modifying the above code to achieve this requirement.
To get the Tiff tags, u don't need to render the image first. If you render the image you will definitely get that exception if any tags are not proper.
Instead try this,
ByteArraySeekableStream sStream= new ByteArraySeekableStream(imageByteArray);
And get the Tiff directory from the stream instead of rendered image
TIFFDirectory tiffDr = new TIFFDirectory(sStream, 0)
From this you can get the Tiff tags, and then do the validations on the available tags in the image. Hope this helps.

Creating PDF using JAVA (Netbeans) with images and multi pages

I am developing a Java program with the following requirements:
The application will take 5 input fields and 3 images (browse and "attach" to the Java application).
Once the "form" is completed it will be submitted using a button called "submit".
Once submitted the JAVA application will create a PDF file with the 5 inputed text and the 3 attached images.
I should be able to control which goes to which page number.
How do I implement such a solution with iText?
The application will take 5 input fields and 3 images (browse and "attach" to the Java application).
Once the "form" is completed it will be submitted using a button called "submit".
These first two requirements are unclear; are they to be implemented in a Java GUI (AWT? Swing? FX?), in some independent web UI (Plain HTML? Vaadin?), or in some derived UI (Portlet? ...)?
But as the question title "Creating PDF using JAVA (Netbeans) with images and multi pages" focuses on the PDF creation, let's look at the third and fourth requirements.
Once submitted the JAVA application will create a PDF file with the 5 inputed text and the 3 attached images.
I should be able to control which goes to which page number.
Let's assume you already have those inputs in the variables
String text1, text2, text3, text4, text5;
byte[] image1, image2, image3;
The framework
With iText you now create the document like this:
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
...
// where you want to create the PDF;
// use a FileOutputStream for creating the PDF in the file system
// use a ByteArrayOutputStream for creating the PDF in a byte[] in memory
OutputStream output = ...;
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, output);
document.open();
// Add content for the first page(s)
...
// Start e new page
document.newPage();
// Add content for the next page(s)
...
// Start a new page
document.newPage();
// etc etc
document.close();
Adding text
You can add text in one of the Add content for the ... page(s) sections using
import com.itextpdf.text.Paragraph;
...
document.add(new Paragraph(text1));
Adding an image
You can add an image in one of the Add content for the ... page(s) sections using
import com.itextpdf.text.Image;
...
document.add(Image.getInstance(image1));
Adding at a given position
Adding text or images as described above leaves the layout details to iText, and iText fills the page from top to bottom except some margins.
If you want to control the positioning of the content yourself (which also means you have to take care that the content parts do not overlap or are drawn outside the page area), you can do so like this:
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.Phrase;
...
PdfContentByte canvas = writer.getDirectContent();
Phrase phrase = new Phrase(text2);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 200, 572, 0);
Image img = Image.getInstance(image2);
img.setAbsolutePosition(200, 200);
canvas.addImage(img);
And there are many more options how to manipulate your content, e.g. choosing a font, choosing text sizes, scaling images, rotating content, ..., simply have a look at the iText samples from the book iText in Action - Second Edition.
You can use XSL-FO. A basic example here. After this, you can search and add other options for your PDF.

Categories

Resources