[
https://issues.apache.org/jira/browse/PDFBOX-5078?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17266489#comment-17266489
]
Joseph commented on PDFBOX-5078:
--------------------------------
[~tilman] Thanks a lot and it works great. Yes, generally the pages will not be
in sequence, so more logic is required. I have done the complete code for
rearrange the page sequence and it works awesome. Please go through so that you
can add a method in splitter or a new class which would do the conversion from
Booklet into PDF
{code:java}
for (File f : items) {
System.out.println();
PDDocument document = PDDocument.load(f);
if (document.isEncrypted()) {
System.err.println("Error: Encrypted documents are not supported!");
System.exit(1);
}
System.out.println("Start: Create PDF from Booklet.");
int noOfPages= document.getNumberOfPages();
System.out.print("No of Pages in the PDF File: " + f.getAbsolutePath() + " are
" + noOfPages);
int j=0;
for(int i=0; i<noOfPages; i++) {
System.out.println("Page No (PDF From Booklet): "+ (i+1));
PDDocument outdoc = new PDDocument();
PDPage page = document.getPage(i);
PDRectangle cropBoxORIG = page.getCropBox();
// make sure to have new objects
PDRectangle cropBoxLEFT = new PDRectangle(cropBoxORIG.getCOSArray());
PDRectangle cropBoxRIGHT = new PDRectangle(cropBoxORIG.getCOSArray());
if (page.getRotation() == 90 || page.getRotation() == 270) {
cropBoxLEFT.setUpperRightY(cropBoxORIG.getLowerLeftY() +
cropBoxORIG.getHeight() / 2);
cropBoxRIGHT.setLowerLeftY(cropBoxORIG.getLowerLeftY() +
cropBoxORIG.getHeight() / 2);
} else {
cropBoxLEFT.setUpperRightX(cropBoxORIG.getLowerLeftX() +
cropBoxORIG.getWidth() / 2);
cropBoxRIGHT.setLowerLeftX(cropBoxORIG.getLowerLeftX() +
cropBoxORIG.getWidth() / 2);
}
PDPage pageLEFT = null;
PDPage pageRIGHT = null;
if (page.getRotation() == 180 || page.getRotation() == 270) {
pageRIGHT = outdoc.importPage(page);
pageRIGHT.setCropBox(cropBoxRIGHT);
pageLEFT = outdoc.importPage(page);
pageLEFT.setCropBox(cropBoxLEFT);
} else {
pageLEFT = outdoc.importPage(page);
pageLEFT.setCropBox(cropBoxLEFT);
pageRIGHT = outdoc.importPage(page);
pageRIGHT.setCropBox(cropBoxRIGHT);
}
j = 1 + j;
outdoc = new PDDocument();
outdoc.importPage(pageLEFT);
outdoc.save(new File(f.getParent(),"sample_" + (j) + ".pdf"));
j = j + 1;
outdoc = new PDDocument();
outdoc.importPage(pageRIGHT);
outdoc.save(new File(f.getParent(),"sample_" + (j) + ".pdf"));
outdoc.close();
/*PDRectangle cropBoxORIG = document.getPage(i).getCropBox();
PDRectangle cropBoxLEFT = new PDRectangle(cropBoxORIG.getCOSArray());
cropBoxLEFT.setUpperRightX(cropBoxORIG.getLowerLeftX() +
cropBoxORIG.getWidth() / 2);
page.setCropBox(cropBoxLEFT);
outdoc.importPage(page);
j = 1 + j;
outdoc.save(new File(f.getParent(),"sample_" + (j) + ".pdf"));
outdoc = new PDDocument();
PDRectangle cropBoxRIGHT = new PDRectangle(cropBoxORIG.getCOSArray());
cropBoxRIGHT.setLowerLeftX(cropBoxORIG.getLowerLeftX() +
cropBoxORIG.getWidth() / 2);
page.setCropBox(cropBoxRIGHT);
outdoc.importPage(page);
j = j + 1;
outdoc.save(new File(f.getParent(),"sample_" + (j) + ".pdf"));
outdoc.close();*/
}
//document.save("Path");
//document.close();
//System.exit(0);
File fileDir = new File(f.getParent());
PDFMergerUtility PDFmerger = new PDFMergerUtility();
//Setting the destination file
String destinationFileName = f.getParent()+ File.separator + f.getName();
destinationFileName= destinationFileName.replace(".pdf","");
PDFmerger.setDestinationFileName(destinationFileName+ "_converted.pdf");
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String fileName) {
if (fileName.startsWith("sample")) {
return true;
}
return false;
}
};
File[] files = fileDir.listFiles(filter);
String destDir = f.getParent() + File.separator + "dest";
String dir = f.getParent();
int totalPages = files.length;
int loops = totalPages/2;
boolean pickFirst = true;
int pageNbr1 = 0;
int pageNbr2 = 0;
int count = 1;
for(int i=1; i <= loops; i++) {
if(pickFirst == true) {
pageNbr1 = totalPages;
pageNbr2 = i;
pickFirst = false;
totalPages--;
} else {
pageNbr1 = i;
pageNbr2 = totalPages;
totalPages--;
pickFirst = true;
}
System.out.println("PageNbr 1 "+pageNbr1);
System.out.println("PageNbr 2 "+pageNbr2);
File dest = new File(destDir);
dest.mkdirs();
File file1 = new File(dir + "sample_"+ pageNbr1+".pdf");
renameFile(file1, dir, count, "sample_", destDir, pageNbr1);
count++;
File file2 = new File(dir + "sample_" + pageNbr2+".pdf");
renameFile(file2, dir, count, "sample_", destDir, pageNbr2);
count++;
}
File splittedDir = new File(f.getParent() + File.separator + "dest");
File[] splittedFiles = splittedDir.listFiles(filter);
Arrays.sort(splittedFiles, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
int n1 = extractNumber(o1.getName());
int n2 = extractNumber(o2.getName());
return n1 - n2;
}
private int extractNumber(String name) {
int i = 0;
try {
int s = name.indexOf("_")+1;
int e = name.lastIndexOf('.');
String number = name.substring(s, e);
i = Integer.parseInt(number);
} catch(Exception e) {
i = 0; // if filename does not match the format
// then default to 0
}
return i;
}
});
for(File file: splittedFiles) {
System.out.println("File NAME: "+file.getAbsolutePath());
}
for(File file: splittedFiles) {
PDFmerger.addSource(file);
}
PDFmerger.mergeDocuments();
System.out.println("Documents merged");
System.out.println("Sample files will be deleted");
File reaarrnagedDir = new File(f.getParent() + File.separator + "dest");
File[] rearrangedFiles = reaarrnagedDir.listFiles(filter);
for(File file: rearrangedFiles) {
file.delete();
file.deleteOnExit();
}
}
public static void renameFile(File file, String dir, int count, String
fileNameStartsWith, String destDir, int pageNbr) {
System.out.println("Name1: " + new File(dir + File.separator +
fileNameStartsWith+ count + ".pdf"));
System.out.println("Rename To: " + new File(destDir + File.separator +
fileNameStartsWith+ pageNbr +".pdf"));
boolean success = new File(dir + File.separator +
fileNameStartsWith+count+".pdf").renameTo(new File(destDir + File.separator +
fileNameStartsWith+pageNbr+".pdf"));
if(!success) {
System.out.println("Not success Actual : "+new File(dir + File.separator +
fileNameStartsWith+count+".pdf"));
System.out.println("Not success Rename To : "+new File(destDir +
File.separator + fileNameStartsWith+pageNbr+".pdf"));
} else {
System.out.println("success");
}
}
{code}
> Failure to modify cropBox when splitting a PDF Page vertically into 2 pieces
> ----------------------------------------------------------------------------
>
> Key: PDFBOX-5078
> URL: https://issues.apache.org/jira/browse/PDFBOX-5078
> Project: PDFBox
> Issue Type: Bug
> Affects Versions: 2.0.22
> Reporter: Joseph
> Assignee: Tilman Hausherr
> Priority: Major
> Fix For: 2.0.23, 3.0.0 PDFBox
>
> Attachments: 58-10-05E Not Working.pdf, 64-1212 (4).pdf,
> sample_2.pdf, screenshot-1.png
>
>
> We can able to convert A4 PDF to Booklet PDF via PrintDF open source
> ([https://github.com/Raudius/PrintDF/blob/master/src/us/raudi/printdf/PrintDF.java)]
> However not able to convert Booklet to A4 i.e that is cutting vertically of a
> Booklet PDF page and resequence all the pages.
> 1st problem is not sure how to cut veritically each and every PDF page and
> save as 2 separate PDF page or file
> 2nd re-sequence of the booklet to proper sequence as 1, 2, 3, 4, 5 by first
> identifying the actual book sequence
> (http://www.boooks.org/index.php?page=21&lng=1)
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]