I know I asked this before. But was just sent to general PDFBOX help pages. Most if not all examples there are "HELLO WORLD" types.
But does ANYONE have an actual working example of placing a PDF inside of another PDF using PDFBOX? Can you share the code if so?
I have a small (A) PDF. I want to place it on the top of another (B) PDF. So when the class runs, I will end up with ONE page. PDF (B) would remain intact as it was before I executed the class, with PDF (A) on top of it. Both PDF (A) small and the other PDF (B) would pre-exist.
Have tried to make multiple examples work that I got from stack overflow.
The one that was published on stack overflow to my question above created both the PDFs in the code. They were not external. When feeding in external PDFs, I got a blank one. But not really sure if my doctoring to that one was why it created a blank page.
My question to stack overflow:
https://stackoverflow.com/questions/47295391/create-a-one-page-pdf-from-two-pdfs-using-pdfbox/47349671?noredirect=1#comment81672358_47349671
My doctored code:
PDDocument document = new PDDocument();
File topfile = new File(args[0]);
PDDocument topSource = PDDocument.load(topfile);
File bottomfile = new File(args[1]);
PDDocument bottomSource = PDDocument.load(bottomfile);
PDDocument target = new PDDocument();
LayerUtility layerUtility = new LayerUtility(target);
PDFormXObject topForm = layerUtility.importPageAsForm(topSource, 0);
PDFormXObject bottomForm = layerUtility.importPageAsForm(bottomSource, 0);
float height = topForm.getBBox().getHeight() + bottomForm.getBBox().getHeight();
float width, topMargin, bottomMargin;
if (topForm.getBBox().getWidth() > bottomForm.getBBox().getWidth()) {
width = topForm.getBBox().getWidth();
topMargin = 0;
bottomMargin = (topForm.getBBox().getWidth() - bottomForm.getBBox().getWidth()) / 2;
} else {
width = bottomForm.getBBox().getWidth();
topMargin = (bottomForm.getBBox().getWidth() - topForm.getBBox().getWidth()) / 2;
bottomMargin = 0;
}
PDPage targetPage = new PDPage(new PDRectangle(width, height));
target.addPage(targetPage);
PDPageContentStream contentStream = new PDPageContentStream(target, targetPage);
if (bottomMargin != 0)
contentStream.transform(Matrix.getTranslateInstance(bottomMargin, 0));
contentStream.drawForm(bottomForm);
contentStream.transform(Matrix.getTranslateInstance(topMargin - bottomMargin, bottomForm.getBBox().getHeight()));
contentStream.drawForm(topForm);
contentStream.close();
document.save(args[2]);
}
}
Other examples have constructors or methods not found (like the code below) and the class cannot be saved and created.
New to Java. Not like I have not been digging and trying for weeks. But just keep hitting the wall with this one. Not like I really understand this code that well either. Copy/Paste /try technology.
Part of the problem too with PDFBOX being free is there is really not a ton of help out there.
My errors with the code below:
List
it is a raw type and should be parameterized.
(getAllPages),
Method is undefined.
PDFObjectForm
Cannot be resolved to a type
AffineTransform,
Cannot resolve to a type.
import org.apache.*;
import org.apache.pdfbox.multipdf.LayerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import java.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class AddAnn2PDF {
public static void main(String[] args) throws IOException {
File BIG_SOURCE_FILE = new File(args[0]);
PDDocument bigDocument = PDDocument.load(BIG_SOURCE_FILE);
File FIRST_SUPER_FILE = new File(args[1]);
LayerUtility layerUtility = new LayerUtility(bigDocument);
List bigPages = bigDocument.getDocumentCatalog().getAllPages();
// import each page to superimpose only once
PDDocument firstSuperDocument = PDDocument.load(FIRST_SUPER_FILE);
PDXObjectForm firstForm = layerUtility.importPageAsForm(firstSuperDocument, 0);
// These things can easily be done in a loop, too
AffineTransform affineTransform = new AffineTransform(); // Identity... your requirements may differ
layerUtility.appendFormAsLayer((PDPage) bigPages.get(0), firstForm, affineTransform, "Superimposed0");
bigDocument.save(args[2]);
}
}
Tom Deskevich
Tom Deskevich
Programmer/Analyst
P: 814-472-6066 x134
F: 814-472-5019
E: TDeskevich@xxxxxxxxxxxxxxxxxx
172 Gateway Road PO Box 568
Ebensburg, Pennsylvania
15931
www.harrislocalgov.com
This message has been sent on behalf
of a company that is part of the Harris Operating Group of
Constellation Software Inc. These companies are listed at the
following link:
http://subscribe.harriscomputer.com
Unsubscribe Option:
If you do not wish to receive any future email, please unsubscribe
by clicking on the following link:
http://subscribe.harriscomputer.com
This message is
intended exclusively for the individual or entity to which it is
addressed. This communication may contain information that is
proprietary, privileged or confidential or otherwise legally exempt
from disclosure. If you are not the named addressee, you are not
authorized to read, print, retain, copy or disseminate this message
or any part of it. If you have received this message in error,
please notify the sender immediately by e-mail and delete all copies
of the
message.
As an Amazon Associate we earn from qualifying purchases.