开发者

Split one PDF page into two

i want to split one wide PDF page into two PDF pages. My original page is wide as two A4 page size but height is normal(for A4). I trying to 开发者_运维技巧use IText but with no effects. Thanks for attention.


I don't know the iText API, but you can follow these steps to get there:

Create two new copies of the existing page. This means that you have the same Resources, the same ContentStream, etc.

Get the MediaBox for the first page which is an array laid out as [llx lly urx ury].

if MediaBox[2] - MediaBox[0] == long edge of A4 page then
    HalfPageWidth = MediaBox[2] - MediaBox[0];
    PageCopy1.CropBox = [MediaBox[0] MediaBox[1] (MediaBox[0] + HalfPageWidth) MediaBox[3]]
    PageCopy2.CropBox = [(MediaBox[0] + HalfPageWidth) MediaBox[1] MediaBox[2] MediaBox[3]]
else
    HalfPageHeight = MediaBox[3] - MediaBox[1];
    PageCopy1.CropBox = [MediaBox[0] MediaBox[1] MediaBox[2] (MediaBox[1] + HalfPageHeight)]
    PageCopy2.CropBox = [MediaBox[0] (MediaBox[1] + HalfPageHeight)] MediaBox[2] MediaBox[3]]

Remove the original page and save these two pages. Basically, you're making two identical copies of the page and cropped each to half the page. You may also need to set the page rotation.


You can also use Ghostscript (with the addition of a PostScript code snippet to the call) for that. Commandlines required:

Output the left side:

 gs \
   -o left-half.pdf \
   -sDEVICE=pdfwrite \
   -g5950x8420 \
   -dFIXEDMEDIA \
   -PDFFitPage \
   -dAutoRotatePages=/None \
   -c "<</PageOffset [0 0]>> setpagedevice" \
    doubleup.pdf

Output the right side:

 gs \
   -o left-half.pdf \
   -sDEVICE=pdfwrite \
   -g5950x8420 \
   -dFIXEDMEDIA \
   -PDFFitPage \
   -dAutoRotatePages=/None \
   -c "<</PageOffset [-595 0]>> setpagedevice" \
    doubleup.pdf

These commandlines can easily be translated into Java or whatever code to use the appropriate GS API calls...


I would look to be creating a copy of the original PDF with the changed page rather than updating the existing one, it'll be easier to work in iText that way and you can always rename the file aftwarads.

Have a look at the HelloWorldCopy example here.

Only thing you will need to change is the call to split the wide page into two pages. So do the same as the HelloWorldCopy example for all pages except the one you want to split - for this page have a look at the alternative PDfCopy.addPage() method that allows you to specify a rectangle that defines the newly created page's size.

So that should allow you to split the wide page into two properly-sized new pages. Now you need to make sure the left-hand part of the wide page goes into the first new page and the right-hand part goes into the second new page. For this you should look at the PdfImportedPage.setMatrix method (PdfImportedPage is the object returned from copy.getImportedPage() in the example.


Sample code is in c# but is pretty similar. I have used it to split one A3 page to 2 A4 pages, you just have to play with x,y values.

    private void CreatePdf(string saveLocation, string bigPageSource)
    {
        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(saveLocation, FileMode.Create));

        document.Open();

        PdfContentByte cb = writer.DirectContent;

        PdfReader reader = new PdfReader(bigPageSource);
        PdfImportedPage page = writer.GetImportedPage(reader, 1);

        document.NewPage();
        cb.AddTemplate(page, 0, 0);

        document.NewPage();
        cb.AddTemplate(page, -PageSize.A4.Width, 0);

        document.Close();
    }


try Briss !

A simple user interface lets you define exactly the crop-region by fitting a rectangle on the visually overlaid pages.

It pairs even and uneven pages, and seperates user selected pages. You could calculate the exact A4 ratio and enter the values manually, rather than drawing a rectangle, and/or align the cropping regions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜