Я пытаюсь изменить размер PDF, чтобы создать свободное пространство внизу для водяного знака нижнего колонтитула.Пространство для нижнего колонтитула необходимо создавать независимо от ориентации и размера страницы.Код, который я реализовал для изменения масштаба страницы, приведен ниже:
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//String pdfFilename = "/MuhimbiPOC/Templates/Source_doc_withheaderfooter.pdf";
//String pdfFilename = "/MuhimbiPOC/Templates/3.pdf";
//String pdfFilename = "C:\\Users\\H176298\\Documents\\Documents\\ERS\\ERS-Build_20.0_Muhimbi\\Sample Files\\Pages_from_annual_report_2009.pdf";
String pdfFilename = "C:\\Users\\H176298\\Documents\\Documents\\ERS\\ERS-Build_20.0_Muhimbi\\Sample Files\\extract_A17_FlightPlan.pdf";
PDDocument document = PDDocument.load(new File(pdfFilename));
PDDocument documentPDF = new PDDocument();
PDFRenderer pdfRenderer = new PDFRenderer(document);
PDPage pge = new PDPage();
int pageCounter = 0;
for (PDPage page : document.getPages())
{
int rotation = page.getRotation();
final PDRectangle mediaBox = page.getMediaBox();
final PDRectangle cropBox = page.getCropBox();
boolean isLandscape = mediaBox.getWidth() > mediaBox.getHeight();
/**
* Code to Detect the Portrait Mode of Page
*/
boolean rotate = rotation == 90 || rotation == 270;
System.out.println("Landscape " + isLandscape);
System.out.println("pageSize.getHeight() :: " + cropBox.getHeight());
System.out.println("pageSize.getWidth() :: " + cropBox.getWidth());
System.out.println("pageSize.getUpperRightX() :: " + cropBox.getUpperRightX());
System.out.println("pageSize.getUpperRightY() :: " + cropBox.getUpperRightY());
System.out.println("pageSize.getLowerLeftX() :: " + cropBox.getLowerLeftX());
System.out.println("pageSize.getLowerLeftY() :: " + cropBox.getLowerLeftY());
if(!rotate && !isLandscape) {
System.out.println("Not Rotation ............................... " + rotate);
mediaBox.setUpperRightX((float) (page.getMediaBox().getUpperRightX()));
mediaBox.setUpperRightY(page.getMediaBox().getUpperRightY() + page.getMediaBox().getHeight() * 0.5f);
mediaBox.setLowerLeftY(page.getMediaBox().getLowerLeftY() - page.getMediaBox().getHeight() * 0.5f);
cropBox.setUpperRightX((float) (page. getCropBox().getUpperRightX()) + (float) (page. getCropBox().getUpperRightX())*0.5f);
cropBox.setUpperRightY(page.getCropBox().getUpperRightY());
cropBox.setLowerLeftX(page.getCropBox().getLowerLeftX() - page.getCropBox().getWidth() * 0.5f);
}else {
/***************************************************************************************/
mediaBox.setUpperRightX((float) (Math.abs(page.getMediaBox().getUpperRightX()) + Math.abs(page.getMediaBox().getWidth()) * 0.1f));
mediaBox.setUpperRightY(Math.abs(page.getMediaBox().getUpperRightY()));
mediaBox.setLowerLeftY(page.getMediaBox().getLowerLeftY());
mediaBox.setUpperRightY(Math.abs(page.getMediaBox().getUpperRightY()) + Math.abs(page.getMediaBox().getHeight()) * 0.1f);
mediaBox.setLowerLeftY(page.getMediaBox().getLowerLeftY() - Math.abs(page.getMediaBox().getHeight()) * 0.1f);
cropBox.setUpperRightX((float) (Math.abs(page. getCropBox().getUpperRightX())) + (float) (Math.abs(page. getCropBox().getUpperRightX()))*0.1f);
cropBox.setUpperRightY(Math.abs(page.getMediaBox().getUpperRightY()));
cropBox.setLowerLeftY(page.getCropBox().getLowerLeftY());
cropBox.setLowerLeftY(page.getCropBox().getLowerLeftY() - Math.abs(page.getCropBox().getHeight() * 0.1f));
cropBox.setLowerLeftX(page.getCropBox().getLowerLeftX() - (float) (page. getCropBox().getLowerLeftX())*0.1f);
System.out.println("Rotation ...............................");
// note that the page number parameter is zero based
}
/****************************************************************************/
System.out.println("==================================== " + rotate);
System.out.println((float) (page.getMediaBox().getWidth()) + " :: " + page.getMediaBox().getHeight());
System.out.println(mediaBox.getWidth() + " :: " + mediaBox.getHeight());
System.out.println("pageSize.getHeight() :: " + cropBox.getHeight());
System.out.println("pageSize.getWidth() :: " + cropBox.getWidth());
System.out.println("pageSize.getUpperRightX() :: " + cropBox.getUpperRightX());
System.out.println("pageSize.getUpperRightY() :: " + cropBox.getUpperRightY());
System.out.println("pageSize.getLowerLeftX() :: " + cropBox.getLowerLeftX());
System.out.println("pageSize.getLowerLeftY() :: " + cropBox.getLowerLeftY());
System.out.println("====================================");
page.setMediaBox(mediaBox);
page.setCropBox(cropBox);
}
System.out.println("No. of Pages :: " + document.getNumberOfPages());
document.save(pdfFilename + "_test.pdf");
System.out.println("Task Completed ... @ " + new Date());
document.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
. Он работает в большинстве случаев, кроме нескольких.Приложите примеры файлов, в которых этот код не работает.
ФАЙЛ ОБРАЗЦОВ PDF 1
ФАЙЛ ОБРАЗЦОВ PDF 2