在将页面大小调整为7.31 x 11大小后,为什么某些内容被裁剪掉了? [英] Why some of the content is getting cropped off after resizing the page to 7.31 x 11 size?
本文介绍了在将页面大小调整为7.31 x 11大小后,为什么某些内容被裁剪掉了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试将页面尺寸调整为7.31 x 11时,该页面中的某些内容已从窗口中裁剪掉.以下是我的输出文档的链接.
When I am trying to resize the page to 7.31 x 11 , some of the content in that page is getting cropped off the window. Below is the link for my output document.
http://www.filedropper.com/mynewdocument
下面是我的源代码
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;
public class PDFConvert {
public static void main(String[] args) throws InvalidPasswordException, IOException, PrinterException {
File file = new File("d:\\pdf\\000009.pdf");
PDDocument document = PDDocument.load(file);
PDPage page1 = document.getDocumentCatalog().getPages().get(0);
PDDocumentOutline outline = document.getDocumentCatalog().getDocumentOutline();
PDRectangle mediaBox = new PDRectangle(7.31f * 72, 11.0f * 72);
PDPage reSizepage = printBookmark(document, outline, "");
if (reSizepage != null) {
reSizepage.setMediaBox(mediaBox);
}
PDDocument doc = new PDDocument();
doc.addPage(page1);
doc.addPage(reSizepage);
doc.save("d:\\pdf\\mynewDocument.pdf");
doc.close();
}
public static PDPage printBookmark(PDDocument document, PDOutlineNode bookmark, String indentation)
throws IOException {
PDOutlineItem current = bookmark.getFirstChild();
while (current != null) {
System.out.println(current.getTitle());
if (current.getDestination() instanceof PDPageDestination) {
if (current.getTitle().equals("Table DP-2. Profile of Selected Social Characteristics: 2000")) {
PDPageDestination pd = (PDPageDestination) current.getDestination();
System.out.println("Destination page: " + (pd.retrievePageNumber()));
return pd.getPage();
}
}
printBookmark(document, current, indentation + " ");
current = current.getNextSibling();
}
return null;
}
}
推荐答案
当然,您的内容被裁剪掉了,这就是您告诉它要做的.您还需要缩小内容,在页面内容流之前添加缩放矩阵:
Of course your content is cropped off, that's what you told it to do. You need to shrink the content too, be prepending a scaling matrix to the page content stream:
if (reSizepage != null)
{
reSizepage.setMediaBox(mediaBox);
PDPageContentStream contentStream = new PDPageContentStream(document, reSizepage, PDPageContentStream.AppendMode.PREPEND, false);
contentStream.transform(Matrix.getScaleInstance(0.9f, 0.9f));
contentStream.close();
}
类似但更好的答案(最后全部重设)此处
similar, but better answer (resets all at the end) here
这篇关于在将页面大小调整为7.31 x 11大小后,为什么某些内容被裁剪掉了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文