Skip to content

Commit f87f545

Browse files
authored
Merge pull request #155 from ArtifexSoftware/docs-update
Fixes code samples for splitting pages and combining pages. Adds Utils.
2 parents 6dc2255 + 0639cc6 commit f87f545

File tree

3 files changed

+57
-56
lines changed

3 files changed

+57
-56
lines changed
File renamed without changes.

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Developer documentation to help you get started
4646
:maxdepth: 2
4747

4848
classes/index.rst
49+
glossary/Utils.rst
4950
glossary/vars.rst
5051
migration/bytescout/index.rst
5152

@@ -54,6 +55,7 @@ Developer documentation to help you get started
5455

5556

5657

58+
5759
Find us
5860
--------------------------------------------------------------------------------------------------
5961

docs/the-basics/index.rst

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,11 @@ To add a blank page, do the following:
721721

722722
.. code-block:: cs
723723
724-
(int w, int h) = Utils.PageSize("letter-l"); // 'Letter' landscape
724+
(int w, int h) = Utils.PaperSize("letter-l"); // 'Letter' landscape
725725
Page page = doc.NewPage(width: w, height: h);
726726
727727
728-
The convenience function :meth:`PageSize` knows over 40 industry standard paper formats to choose from. To see them, inspect dictionary :attr:`paperSizes`. Pass the desired dictionary key to :meth:`PageSize` to retrieve the paper dimensions. Upper and lower case is supported. If you append "-L" to the format name, the landscape version is returned.
728+
The convenience function :meth:`PaperSize` knows over 40 industry standard paper formats to choose from. To see them, inspect dictionary :attr:`paperSizes`. Pass the desired dictionary key to :meth:`PageSize` to retrieve the paper dimensions. Upper and lower case is supported. If you append "-L" to the format name, the landscape version is returned.
729729

730730
Here is a 3-liner that creates a |PDF|: with one empty page. Its file size is 460 bytes:
731731

@@ -739,7 +739,7 @@ To add a blank page, do the following:
739739
**API reference**
740740

741741
- :meth:`Document.NewPage`
742-
- :meth:`Utils.PageSize`
742+
- :ref:`PaperSize`
743743

744744

745745
----------
@@ -790,62 +790,55 @@ Using the :meth:`Document.InsertPage` method also inserts a new page and accepts
790790
Splitting Single Pages
791791
~~~~~~~~~~~~~~~~~~~~~~~~~~
792792

793-
This deals with splitting up pages of a |PDF| in arbitrary pieces. For example, you may have a |PDF| with *Letter* format pages which you want to print with a magnification factor of four: each page is split up in 4 pieces which each going to a separate |PDF| page in *Letter* format again.
793+
Splitting considers creating new |PDF| documents from an existing input file. To split we need to find the pages we are interested in and insert them into a new |PDF| document.
794794

795795

796+
Example #1 - Split document by each page
797+
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
796798

797799
.. code-block:: cs
798800
799801
using MuPDF.NET;
800802
801-
Document src = pymupdf.open("test.pdf");
802-
Document doc = pymupdf.open(); // empty output PDF
803+
Document src = new Document("test.pdf"); // open a document
803804
804805
for (int i = 0; i < src.PageCount; i ++) // for each page in input
805-
Page spage = doc[i];
806-
r = spage.rect; // input page rectangle
807-
d = new Rect(spage.CropboxPosition, // CropBox displacement if not
808-
spage.CropboxPosition) // starting at (0, 0)
809-
//--------------------------------------------------------------------------
810-
// example: cut input page into 2 x 2 parts
811-
//--------------------------------------------------------------------------
812-
r1 = r / 2; // top left rect
813-
r2 = r1 + (r1.width, 0, r1.width, 0); // top right rect
814-
r3 = r1 + (0, r1.height, 0, r1.height); // bottom left rect
815-
r4 = new Rect(r1.br, r.br); // bottom right rect
816-
List<Rect> rect_list = new List<Rect>([r1, r2, r3, r4]); // put them in a list
817-
818-
for(Rect rx in rect_list) // run thru rect list
819-
{
820-
rx += d; // add the CropBox displacement
821-
page = doc.NewPage(-1, // new output page with rx dimensions
822-
width: rx.width,
823-
height: rx.height);
824-
page.ShowPdfPage(
825-
page.Rect, // fill all new page with the image
826-
src, // input document
827-
spage.Number, // input page number
828-
clip: rx, // which part to use of input page
829-
);
830-
}
806+
{
807+
Page page = src[i];
808+
Document splitDocument = new Document();
809+
splitDocument.InsertPdf(src, fromPage: i, toPage: i);
810+
splitDocument.Save("test-"+i+ ".pdf");
811+
}
831812
832-
// that's it, save output file
833-
doc.Save("poster-" + src.Name,
834-
garbage: 3, // eliminate duplicate objects
835-
deflate: true, // compress stuff where possible
836-
);
837813
838814
839-
Example:
815+
Example #2 - Split document pages by bookmark
816+
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
817+
818+
.. code-block:: cs
819+
820+
using MuPDF.NET;
821+
822+
Document src = new Document("test.pdf"); // open a document
823+
var toc = src.GetToc();
824+
825+
foreach (var item in toc)
826+
{
827+
Console.WriteLine("title=" + item.Title);
828+
Console.WriteLine("item=" + item.Page);
829+
Document splitDocument = new Document();
830+
splitDocument.InsertPdf(src, fromPage: item.Page, toPage: item.Page);
831+
splitDocument.Save("test-"+item.Title+ ".pdf");
832+
}
833+
840834
841-
.. image:: ../images/img-posterize.png
842835
843836
.. note::
844837

845838
**API reference**
846839

847-
- :meth:`Page.CropboxPosition`
848-
- :meth:`Page.ShowPdfPage`
840+
- :meth:`Document.InsertPdf`
841+
849842

850843

851844
--------------------------
@@ -864,36 +857,42 @@ This deals with joining |PDF| pages to form a new |PDF| with pages each combinin
864857
865858
using MuPDF.NET;
866859
867-
Document src = new Document("test.pdf");
860+
Document src = new Document("example.pdf");
868861
Document doc = new Document(); // empty output PDF
869-
870-
(int width, int height) = pymupdf.PageSize("a4"); // A4 portrait output page format
862+
(int width, int height) = Utils.PaperSize("a4");
871863
Rect r = new Rect(0, 0, width, height);
872864
873865
// define the 4 rectangles per page
874-
Rect r1 = r / 2; // top left rect
875-
Rect r2 = r1 + (r1.width, 0, r1.width, 0); // top right
876-
Rect r3 = r1 + (0, r1.height, 0, r1.height); // bottom left
877-
Rect r4 = pymupdf.Rect(r1.br, r.br); // bottom right
866+
Rect r1 = new Rect(0, 0, r.Width / 2, r.Height / 2); // top left rect
867+
Rect r2 = new Rect(r.Width / 2, 0, r.Width, r.Height / 2); // top right
868+
Rect r3 = new Rect(0, r.Height / 2, r.Width / 2, r.Height); // bottom left
869+
Rect r4 = new Rect(r.Width / 2, r.Height / 2, r.Width, r.Height); // bottom right
878870
879871
// put them in a list
880-
Rect[] r_tab = new Rect[]{ r1, r2, r3, r4 };
872+
Rect[] r_tab = new Rect[] { r1, r2, r3, r4 };
873+
Page? page = null;
881874
882875
// now copy input pages to output
883876
for (int i = 0; i < src.PageCount; i++)
884877
{
885-
Page spage = doc[i]
886-
if (spage.Number % 4 == 0) // create new output page
878+
if (i % 4 == 0) // create new output page
879+
{
887880
page = doc.NewPage(-1,
888881
width: width,
889-
height: height)
882+
height: height);
883+
}
884+
890885
// insert input page into the correct rectangle
891-
page.ShowPdfPage(r_tab[spage.Number % 4], // select output rect
892-
src, // input document
893-
spage.Number) // input page number
886+
if (page != null)
887+
{
888+
page.ShowPdfPage(r_tab[i % 4], // select output rect
889+
src, // input document
890+
i); // input page number
891+
}
894892
}
893+
895894
// by all means, save new file using garbage collection and compression
896-
doc.Save("4up.pdf", garbage: 3, deflate: true)
895+
doc.Save("4up.pdf", garbage: 3, deflate: 1);
897896
898897
899898
Example:

0 commit comments

Comments
 (0)