pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sébastien Boutté <sebastien.bou...@gmail.com>
Subject Re: Page Merging Or Rectangle Rotation
Date Sun, 18 Nov 2012 23:57:29 GMT
Hi Everyone,

After searching so many hours (10h, 12h ?? api too complex for me ?) , to
solve my problem of "merging" my report i've finally found a solution.

I found this page on the internet :
http://stackoverflow.com/questions/9517719/how-can-i-stitch-two-pdf-pages-together-with-pdfbox-java
The code can also be found in TestLayerUtility (
http://svn.apache.org/repos/asf/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/util/TestLayerUtility.java
)

So i modified my report to separate recto from the verso :

Original has Dispositions : Recto1 Verso1 Recto2 Verso2 ...
Even disposition : Recto1 Recto2 ...
Odd Disposition : Verso1 Verso2 ...
Final : Recto1(Rotate180)+Verso1 Recto2(Rotate180)+Verso2

I've to do this on the final document because i want to be able to fold the
paper in 2 parts, then 3 parts for doing a paper-card.
With PdfBox 1.7.1, when i open the document, the document is well produced
but there is an "Acrobat error" at the openning.
With PdfBox 1.6.0, all is OK.

Can someone confirm this ? it seems that PdfBox 1.7.1 has a bug with layer
; i've joined the code below.
For the developers of pdfbox, i think that you shoud add an example showing
how to use layers more in depth ; it will be very appreciated.

Good night

Sébastien Boutté

The documents produced :

Even : https://docs.google.com/open?id=0BwkpSTFEip9CemJoQkFzUjFFeEE
Odd : https://docs.google.com/open?id=0BwkpSTFEip9CdDhKTHJoT1lqS0U
Original : https://docs.google.com/open?id=0BwkpSTFEip9CZ0lTdE9kZldHaEk
Final : https://docs.google.com/open?id=0BwkpSTFEip9CR3Jnci1YZU15ZFk

here is the pseudo code :

Report report = new Report();
report.generate();  // Produce Original File By Birt
File finalFile = report.postProcess();

public File postProcess() {
    PDDocument evenPages = PDDocument.load(getFile());
    PDDocument oddPages = PDDocument.load(getFile());

// We create a pdf for even pages and odd pages to facilitate merge
PdfBox.removeEvenPages(oddPages);
    PdfBox.removeOddPages(evenPages);
 // Rotate oddPage
    PdfBox.rotatePages(evenPages, 180);

    LayerUtility lu = new LayerUtility(oddPages);
    for(int current= 0 ;
current<evenPages.getDocumentCatalog().getAllPages().size();current++) {
      PDXObjectForm form = lu.importPageAsForm(evenPages, current);
      PDPage page = (PDPage)
oddPages.getDocumentCatalog().getAllPages().get(current);
      AffineTransform at = new AffineTransform();
      lu.appendFormAsLayer(page, form, at, "layer" + current); // Layer
should be unique
    }

    File file = File.createTempFile("Report", "." + getReportFormat());
    oddPages.save(file.getCanonicalPath());
    oddPages.close();
    return file;
}

public static void rotatePages(PDDocument pdDocument, int rotation) {
List<PDPage> pageList = (List<PDPage>)
pdDocument.getDocumentCatalog().getAllPages();
for (PDPage current : pageList) {
  current.setRotation(rotation);
}
}

public static void removeOddPages(PDDocument pdDocument) {
List<PDPage> pageList = (List<PDPage>)
pdDocument.getDocumentCatalog().getAllPages();
int page = 0;
for (PDPage current : pageList) {
  if (page % 2 != 0) {
pdDocument.removePage(current);
  }
  page++;
}
}

public static void removeEvenPages(PDDocument pdDocument) {
List<PDPage> pageList = (List<PDPage>)
pdDocument.getDocumentCatalog().getAllPages();
int page = 0;
for (PDPage current : pageList) {
  if (page % 2 == 0) {
pdDocument.removePage(current);
  }
  page++;
}
}

On Sat, Nov 17, 2012 at 1:41 AM, Sébastien Boutté <
sebastien.boutte@gmail.com> wrote:

> In fact, i know the dimensions of the rectangles i have to rotate.
> The pdf is produced by birt ; but with birt it's difficult to produce
> rotated text or objects
> (you have to enhance the designer ... more work to do)
> So this is why, i want to postProcess my report with pdfbox.
>
> Have you a pseudo code with (PDPageNode, ...)  ? cos i try but i've not
> found the solution or any example
>
> Thanks a lot
>
> Sebastien
>
>
> On Fri, Nov 16, 2012 at 6:15 PM, Duane Nickull <
> duane@technoracle-systems.com> wrote:
>
>> You can pretty much do all of this with PDFBox.  When you say you have a
>> PDF divided into two parts though, it is important to understand how you
>> made tho division to see if PDFBox has a method to deal with that
>> mechanism.  Otherwise, you can easily get the height, width then divide
>> based on dimensions or look at the media box.   The results here may vary
>> however if you are processing many of these and they are
>> non-deterministic.  I am presuming you are not writing this for converting
>> a single document.
>>
>> The PDPageNode class has everything you need including setRotation(Int
>> int);
>>
>> Duane Nickull
>> ***********************************
>> Technoracle Advanced Systems Inc.
>> Consulting and Contracting; Proven Results!
>> i.  Neo4J, PDF, Java, LiveCycle ES, Flex, AIR, CQ5 & Mobile
>> b. http://technoracle.blogspot.com
>> t.  @duanechaos
>> "Don't fear the Graph!  Embrace Neo4J"
>>
>>
>>
>>
>>
>>
>> On 2012-11-16 8:51 AM, "Sébastien Boutté" <sebastien.boutte@gmail.com>
>> wrote:
>>
>> >Hi,
>> >
>> >I've got a pdf divided into two parts (Top / Bottom)
>> >I would like to make a new pdf with the top part rotated by 180 degrees.
>> >How can i do this with pdfbox ?
>> >
>> >Another possibility :
>> >I can also make the pdf in 2 pages ; top on the right position of the
>> >first
>> >page
>> >and bottom on the right position of the second page.
>> >Here, is to make the 2 pages merged in one page. (i can rotate first page
>> >with pdfbox)
>> >How can i do this with pdfbox ?
>> >
>> >Thanks for your time,
>> >
>> >Sebastien Boutte
>>
>>
>>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message