Divide one PDF page into two

I want to split one wide page PDF into two PDF pages. My original page is wide, like two A4 sizes, but the height is normal (for A4). I am trying to use IText but with no effects. Thanks for attention.

+7
java pdf itext
source share
5 answers

I do not know the iText API, but you can follow these steps:

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]] 

Delete the original page and save these two pages. Basically, you make two identical copies of the page and crop each half of the page. You may also need to set the page rotation.

+2
source share

I would like to create a copy of the original PDF with the modified page, and not update the existing one, it will be easier to work in iText, and you can always rename the aftwarads file.

Take a look at the HelloWorldCopy example here .

The only thing you need to change is the call to split the wide page into two pages. Just like the HelloWorldCopy example for all pages except the one you want to split, for this page we will consider an alternative method PDfCopy.addPage() , which allows you to specify the rectangle that defines the page size just created.

Thus, this will allow you to split the wide page into two new pages with the appropriate size. Now you need to make sure that the left part of the wide page is included in the first new page, and the right part goes to the second new page. To do this, you should look at the PdfImportedPage.setMatrix method ( PdfImportedPage is the object returned from copy.getImportedPage() in the example.

+1
source share

The sample code is in C #, but pretty similar. I used it to split one A3 page into 2 A4 pages, you just need to play with the 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(); } 
+1
source share

You can also use Ghostscript (with the addition of a snippet of PostScript code to invoke). Required command lines:

Print left side:

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

Print the right side:

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

These command lines can be easily translated into Java or any other code to use the appropriate GS API calls ...

+1
source share

try briss !

A simple user interface allows you to accurately determine the crop area by setting a rectangle on visually overlay pages.

It combines even and uneven pages and separates user-selected pages. You can calculate the exact A4 size and enter the values ​​manually, rather than draw a rectangle and / or align crop areas.

+1
source share

All Articles