You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Michael Klink (JIRA)" <ji...@apache.org> on 2014/10/13 12:02:34 UTC
[jira] [Comment Edited] (PDFBOX-2197) Add sample how to import a
page as PDFormXObject
[ https://issues.apache.org/jira/browse/PDFBOX-2197?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14169149#comment-14169149 ]
Michael Klink edited comment on PDFBOX-2197 at 10/13/14 10:01 AM:
------------------------------------------------------------------
I posted a proof-of-concept based on the current 2.0.0 SNAPSHOT on stackoverflow, cf. [this SO answer|http://stackoverflow.com/a/26299557/1729265]. The original poster of the stackoverflow question pointed me to this issue here. The essentials:
A method to import a page from some document into a target document as form xobject:
{code:title=importAsXObject helper}
PDFormXObject importAsXObject(PDDocument target, PDPage page) throws IOException
{
final PDStream src = page.getContents();
if (src != null)
{
final PDFormXObject xobject = new PDFormXObject(target);
OutputStream os = xobject.getPDStream().createOutputStream();
InputStream is = src.createInputStream();
try
{
IOUtils.copy(is, os);
}
finally
{
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
xobject.setResources(page.findResources());
xobject.setBBox(page.findCropBox());
return xobject;
}
return null;
}
{code}
A sample using that helper method to import a page from some document as an XObject and put it onto a page twice with different rotations and scales.
{code:title=importAsXObject example}
PDDocument source = PDDocument.loadNonSeq(SOURCE, null);
List<PDPage> pages = source.getDocumentCatalog().getAllPages();
PDDocument target = new PDDocument();
PDPage page = new PDPage();
PDRectangle cropBox = page.findCropBox();
page.setResources(new PDResources());
target.addPage(page);
PDFormXObject xobject = importAsXObject(target, pages.get(0));
page.getResources().addXObject(xobject, "X");
PDPageContentStream content = new PDPageContentStream(target, page);
AffineTransform transform = new AffineTransform(0, 0.5, -0.5, 0, cropBox.getWidth(), 0);
content.drawXObject(xobject, transform);
transform = new AffineTransform(0.5, 0.5, -0.5, 0.5, 0.5 * cropBox.getWidth(), 0.2 * cropBox.getHeight());
content.drawXObject(xobject, transform);
content.close();
target.save(TARGET);
target.close();
source.close();
{code}
For a source page like this
!http://i.stack.imgur.com/yDdjI.png!
the result is this:
!http://i.stack.imgur.com/Zb9lf.png!
As mentioned above this is only a proof of concept, neither have corner cases been taken into account nor is the code optimized.
was (Author: mkl):
I posted a proof-of-concept based on the current 2.0.0 SNAPSHOT on stackoverflow, cf. [this SO answer|http://stackoverflow.com/a/26299557/1729265]. The original poster of the stackoverflow question pointed me to this issue here. The essentials:
A method to import a page from some document into a target document as form xobject:
{code:title=importAsXObject helper}
PDFormXObject importAsXObject(PDDocument target, PDPage page) throws IOException
{
final PDStream src = page.getContents();
if (src != null)
{
final PDFormXObject xobject = new PDFormXObject(target);
OutputStream os = xobject.getPDStream().createOutputStream();
InputStream is = src.createInputStream();
try
{
IOUtils.copy(is, os);
}
finally
{
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
xobject.setResources(page.findResources());
xobject.setBBox(page.findCropBox());
return xobject;
}
return null;
}
{code}
A sample using that helper method to import a page from some document as an XObject and put it onto a page twice with different rotations and scales.
{code:title=importAsXObject example}
PDDocument source = PDDocument.loadNonSeq(SOURCE, null);
List<PDPage> pages = source.getDocumentCatalog().getAllPages();
PDDocument target = new PDDocument();
PDPage page = new PDPage();
PDRectangle cropBox = page.findCropBox();
page.setResources(new PDResources());
target.addPage(page);
PDFormXObject xobject = importAsXObject(target, pages.get(0));
page.getResources().addXObject(xobject, "X");
PDPageContentStream content = new PDPageContentStream(target, page);
AffineTransform transform = new AffineTransform(0, 0.5, -0.5, 0, cropBox.getWidth(), 0);
content.drawXObject(xobject, transform);
transform = new AffineTransform(0.5, 0.5, -0.5, 0.5, 0.5 * cropBox.getWidth(), 0.2 * cropBox.getHeight());
content.drawXObject(xobject, transform);
content.close();
target.save(TARGET);
target.close();
source.close();
{code}
For a source page like this
!http://i.stack.imgur.com/yDdjI.png!
the result is this:
!http://i.stack.imgur.com/Zb9lf.png!
As mentioned above this is only a proof of concept, corner cases have not yet been taken into account, neither is the code optimized.
> Add sample how to import a page as PDFormXObject
> ------------------------------------------------
>
> Key: PDFBOX-2197
> URL: https://issues.apache.org/jira/browse/PDFBOX-2197
> Project: PDFBox
> Issue Type: Task
> Components: Documentation
> Affects Versions: 2.0.0
> Reporter: Maruan Sahyoun
> Assignee: Maruan Sahyoun
> Priority: Trivial
> Fix For: 2.0.0
>
>
> Add an example to the pdfbox examples how to import a page from another document into a master document as PDFormXObject
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)