PDFBox : PDPageContentStream's append mode misbehaving
Use the constructor that has a fifth parameter, so to reset the graphic context.
public PDPageContentStream(PDDocument document, PDPage sourcePage, boolean appendContent,
boolean compress, boolean resetContext) throws IOException
alternatively, save and restore the graphics state in the first content stream by calling
saveGraphicsState();
// ...
restoreGraphicsState();
Problem with empty page when using Apache PDFBox to add image to PDF
Please also have a look at the JavaDocs and sources of the library you try to work with. You create a PDPageContentStream
:
PDPageContentStream contents = new PDPageContentStream(doc, page);
This conductor is documented to overwrite all existing content streams of this page:
/**
* Create a new PDPage content stream. This constructor overwrites all existing content streams
* of this page.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage) throws IOException
Thus, you have to use a different constructor which keeps the current page contents, e.g.
/**
* Create a new PDPage content stream.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @param appendContent Indicates whether content will be overwritten, appended or prepended.
* @param compress Tell if the content stream should compress the page contents.
* @param resetContext Tell if the graphic context should be reset. This is only relevant when
* the appendContent parameter is set to {@link AppendMode#APPEND}. You should use this when
* appending to an existing stream, because the existing stream may have changed graphic
* properties (e.g. scaling, rotation).
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent,
boolean compress, boolean resetContext) throws IOException
Thus
PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
should make your code work as desired.
Alternatively, if you want the image in the background, try
PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.PREPEND, true, true);
Beware, though, in certain cases the image won't be visible in the background, e.g. if the existing content starts with an instruction to fill the whole page area in white. In such a case watermarks must be applied with some kind of transparency atop existing content.
Insert Image to existing non-empty pdf
Try to use the append-mode
//creating the PDPageContentStream object
PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, true);
Edit
TilmanHausherr mentioned
new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
Thats why
PDFBox text matrix scaling behavior
It appears that this was the issue. I did not see the constructor with the 5th argument for resetContext
before. I'm still unsure how you would get the current context if you for some reason needed to do something relative to that context, though. In my case, adding the 5th argument solves the problem.
PDFBox : PDPageContentStream's append mode misbehaving
How can I determine that a string contains at least one alphabetic character?
Try this
"^.*[a-zA-Z].*$"
This will work with most regex engines. However it is limited to ASCII alphabetical characters. For international characters we'd need to know the regex engine involved.
Related Topics
How to Run an R Program from Java
Java Object Analogue to R Data.Frame
Libjvm.So: Cannot Open Shared Object File: No Such File or Directory
Loading Data Bigger Than the Memory Size in H2O
Android Gps Incorrect Location Data on Query
Why Does My Compare Method Throw Exception -- Comparison Method Violates Its General Contract!
Trivial: Get Confirmation of Email Sent in Android
Pdfbox:Pdpagecontentstream's Append Mode Misbehaving
How to Install "Android Support Library" to Deploy a Gluon Mobile Application to Android
Aes Gcm Implementation with Authentication Tag in Java
How to Install Rjava in R 3.0 in Ubuntu 13.04
Urlconnection Getcontentlength() Is Returning a Negative Value