I want to change the background color of an already present pdf to transparent or white, and I am using pdfBox for performing other tasks on the pdf, I found some documentation here: setBackroundColor - pdfBox But I am completely unaware of how to use it as I am not accustomed to java.

Can someone possibly provide some example code on doing it ?

有帮助吗?

解决方案

I want to change the background color of an already present pdf to transparent or white

According to the PDF specification ISO 32000-1, section 11.4.7:

Ordinarily, the page shall be imposed directly on an output medium, such as paper or a display screen. The page group shall be treated as an isolated group, whose results shall then be composited with a backdrop colour appropriate for the medium. The backdrop is nominally white, although varying according to the actual properties of the medium. However, some conforming readers may choose to provide a different backdrop, such as a checker board or grid to aid in visualizing the effects of transparency in the artwork.

PDF viewers most often do use this white backdrop. Thus, if your PDF on standard viewers displays a different color in the back, this normally is due to some area filling operation(s) somewhere in the page content stream.

Thus, there is not a simple single attribute of the PDF to set somewhere but instead you have to parse the page content, find the operations which paint what you perceive as background, and change them. There are numerous different operations which may be used for this task, though, and these operations may also be used for other purposes than background coloring. Thus, there is not the method to change backgrounds.

If you have a single specific PDF or PDFs generated alike, please provide a sample document to allow helping you to find find a way to change the perceived background color.

PS: The PDLayoutAttributeObject.setBackgroundColor method you found refers to the creation of so called Layout Attributes which

specify parameters of the layout process used to produce the appearance described by a document’s PDF content. [...]

NOTE The intent is that these parameters can be used to reflow the content or export it to some other document format with at least basic styling preserved.

(section 14.8.5.4 in the PDF specification ISO 32000-1)

Thus, they are provided only in PDFs intended for content reflow or content export and are not used by regular PDF viewers.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top