I try to create a PDF file with PDFBox and then to create from it an image using commercial library jPDFImages (Qoppa software). Yes, i know that PDFBox is able too to create images from PDFs, but for some reasons I need to use commercial library.
I created PDF file and pass it to jPDFImajes, but I have an error: "Unable to find PDF trailer". Qoppa software describe this error.
The problem is seems to be in the PDF trailer, which is created by PDFBox, but I don`t understand how to set it up in right mode? (I have a problem only with PDFs created with PDFBox)
Here is my code for pdf creation:
public void createPDFFromImage( String file) throws Exception {
PDDocument doc = null;
try {
doc = new PDDocument();
BufferedImage bufferedImage = ImageIO.read(new File(/home/.../files/test.png));
PDPage page = new PDPage();
doc.addPage( page );
PDJpeg ximage = new PDJpeg(doc,bufferedImage, (float) 0.95);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.drawXObject(ximage, x, y, W, H);
contentStream.close();
doc.save(file);
} finally {
if( doc != null ) {
doc.close();
}
}
}
Here is an error from commercial library:
java.lang.RuntimeException: com.qoppa.pdf.PDFException: Unable to find PDF trailer.
Caused by: com.qoppa.pdf.PDFException: Unable to find PDF trailer.
I think the problem is, how do I create pdf. Maybe I need to add some information to the pdf to make it valid?
Related
First google result takes me to Add multiple images into a single pdf file with iText using java which was posted 5 years ago. I am not sure which version they are using, because the Image object doesn't even have the getInstance method for me. Needless to say I am not getting much help from that link.
Anyways I am trying to create a javaFX application that loops multiple JPG images to create a single PDF document. Below is my code, which successfully creates a PDF from 2 images, but I am having trouble making the second image display on the second page.
In the link I posted above the simple solution I saw was to do document.newPage() then do document.add(img), but my document object doesn't have that method? I am not sure what to do.
PdfWriter writer = new PdfWriter("D:/sample1.pdf");
// Creating a PdfDocument
PdfDocument pdfDoc = new PdfDocument(writer);
// Adding a new page
// I can add multiple pages here, but when I add multiple images they do not
// automatically flow over to the next page.
pdfDoc.addNewPage();
pdfDoc.addNewPage();
// Creating a Document
Document document = new Document(pdfDoc);
String imageFile = "C:/Users/***/Downloads/MAT204/1.3-1.4 HW/test.jpg";
ImageData data = ImageDataFactory.create(imageFile);
Image img = new Image(data);
img.setAutoScale(true);
img.setRotationAngle(-Math.toRadians(90));
// I can add multiple images, but they overlaps each other and only
// appears on the first page.
// Is there a way for me to change the current page to write on?
document.add(img);
document.add(img);
// Closing the document
document.close();
System.out.println("PDF Created");
Anyways, I just want to figure out how to manually add another image before I write a loop to automate the process.
After doing more research I found the answer here.
https://kb.itextpdf.com/home/it7kb/examples/multiple-images
protected void manipulatePdf(String dest) throws Exception {
Image image = new Image(ImageDataFactory.create(IMAGES[0]));
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(image.getImageWidth(), image.getImageHeight()));
for (int i = 0; i < IMAGES.length; i++) {
image = new Image(ImageDataFactory.create(IMAGES[i]));
pdfDoc.addNewPage(new PageSize(image.getImageWidth(), image.getImageHeight()));
image.setFixedPosition(i + 1, 0, 0);
doc.add(image);
}
doc.close();
}
Im trying to add a TIFF image (CCIT Group 3) to a PDF using Java and PDFBox 1.8.10. There is an image shown on the output file, but its displayed wrong. Its only some black and white pixels.
String outputPath = "/tmp/PDFImage.pdf";
String imagePath = "/tmp/header.tif";
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
PDXObjectImage ximage = new PDCcitt(doc, new RandomAccessFile(new File(imagePath), "r"));
content.drawImage(ximage, 0, 500);
content.close();
doc.save(outputPath);
doc.close();
The PDFBox dependencies says : To write TIFF images a JAI ImageIO Core library will be needed.
I imported the library and and scanned for plugins, but dont found an example how to use is exactly. Someone can help ?
I am integrating my RESTful service to support download of documents etc. For that I am exploring PDFBox library which has capability to work with PDF docs (fantastic) and it works OK if I have to create and save documents.
My example code looks like following;
PDDocument doc = null;
PDPage page = null;
try{
doc = new PDDocument();
page = new PDPage();
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA;
PDPageContentStream content = new PDPageContentStream(doc,page);
content.beginText();
content.drawString("Some Content Received At Runtime");
content.endText();
content.close();
doc.save("SomeName.pdf");
doc.close();
// Now load and return the stream
return PDDocument.load("SomeName.pdf").getDocument().createCOSStream().getFilteredStream();
} catch (Exception e) {
// Do nothing for now
}
If you see in above example I have an option to convert document into InputStream but to achieve this I first have to Save the document and then reload! This is not desirable because this will clutter the server I am running on with junk.
What I really want is to achieve this without saving the document! Is it possible? Should I be looking at some other library like iText? If you know an example for the same please share.
I'm writing a java app that creates a pdf from scratch using the pdfbox library.
I need to place a jpg image in one of the page.
I'm using this code:
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
/* ... */
/* code to add some text to the page */
/* ... */
InputStream in = new FileInputStream(new File("c:/myimg.jpg"));
PDJpeg img = new PDJpeg(document, in);
contentStream.drawImage(img, 100, 700);
contentStream.close();
document.save("c:/mydoc.pdf");
When I run the code, it terminates successfully, but if I open the generated pdf file using Acrobat Reader, the page is completely white and the image is not placed in it.
The text instead is correctly placed in the page.
Any hint on how to put my image in the pdf?
Definitely add the page to the document. You'll want to do that, but I've also noticed that PDFBox won't write out the image if you create the PDPageContentStream BEFORE the PDJpeg. It's unexplained why this is so, but if you look close at the source of ImageToPDF that's what they do. Create the PDPageContentStream after PDJpeg and it magically works.
...
PDJpeg img = new PDJpeg(document, in);
PDPageContentStream stream = new PDPageContentStream( doc, page );
...
Looks like you're missing just a document.addPage(page) call.
See also the ImageToPDF example class in PDFBox for some sample code.
this is how default constructor for PDPageContentStream looks like:
public PDPageContentStream(PDDocument document, PDPage sourcePage) throws IOException
{
this(document, sourcePage, AppendMode.OVERWRITE, true, false);
}
Problem is AppendMode.OVERWRITE for me using another constructor with parameter PDPageContentStream.AppendMode.APPEND resolved a problem
For me this worked:
PDPageContentStream contentStream =
new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);
I'm having trouble using Sun's PDF Renderer package to view PDFs with embedded fonts. I have the following code which creates a BufferedImage out of every page of a PDF for viewing in my application, and it works fine when there are no embedded fonts. However, when the PDF has embedded fonts, it shows no text. Any ideas? Also, it opens fine in Adobe's PDF viewer.
File f = new File("C:\\test.pdf");
FileChannel fc = new RandomAccessFile(f, "r").getChannel();
PDFFile pdfFile = new PDFFile(fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()));
for(int x=0; x<pdfFile.getNumPages(); x++) {
try {
BufferedImage bi = (BufferedImage)pdfFile.getPage(x+1).getImage(
(int)pdfFile.getPage(x+1).getWidth(),
(int)pdfFile.getPage(x+1).getHeight(),
new Rectangle((int)pdfFile.getPage(x+1).getWidth(),
(int)pdfFile.getPage(x+1).getHeight()),
null, true, true);
}
catch (Exception e) {
e.printStackTrace();
}
}
I figured this out by changing PDF renders from PDFRenderer to PDFBox, which works much better. More info is available here.
You could also look at Icesoft, IText, JPedal, and Multivalent who offer Open Source PDF tools.