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
Below is my source code
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
来源:https://stackoverflow.com/questions/45001192/why-some-of-the-content-is-getting-cropped-off-after-resizing-the-page-to-7-31-x