iTextSharp - Combining multiple A4 documents into double-sided A3 booklet

Marcel

I have a PDF form that is 4 x A4 pages. I complete the fields using iTextSharp. All good with this part.

I then need to combine a number of these forms into one PDF document. Plan on using this approach : Using iTextSharp to generate multiple page PDF from existing PDF Form.

These A4 pages however, need to be combined into a single A3 sheet, printed in a "booklet" type configuration, i.e. front and back like below

          A3 Front    ----- turn over ---->    A3 Back
-------------------------------   -------------------------------
|              |              |   |              |              |
|              |              |   |              |              |
|              |              |   |              |              |
|     Page     |     Page     |   |     Page     |     Page     |
|       4      |      1       |   |      2       |       3      |
|              |              |   |              |              |
|              |              |   |              |              |
|              |              |   |              |              |
-------------------------------   -------------------------------

(ascii ftw)

When the A3 is then folded in half, it would read (left to right), page 1, page 2, page 3, page 4. I would then combine all my completed forms in this manner, then print the whole PDF document on A3 back-to-back.

I found this post (http://forums.asp.net/t/1692347.aspx?Merging+two+pdf+pages+into+one+using+itextsharp) that mentions putting a PdfPTable on the page, then grabbing an Image of the page, and embedding it in the table cell...

If I did it this, wouldn't the PDF file be massive, since its basically full of images? Is there a better way to achieve this?

Marcel

Figured it out... follow this approach:

using (var copyms = new MemoryStream())
{
    var document = new Document();

    using (PdfSmartCopy copy = new PdfSmartCopy(document, copyms))
    {
        document.Open();

        foreach (var item in Items)
        {
            // Read the template
            var pdfReader = new PdfReader(TemplateLocation);

            // Save the current completed template to a MemoryStream
            using (var ms = new MemoryStream())
            {
                using (PdfStamper stamper = new PdfStamper(pdfReader, ms))
                {
                    var fields = stamper.AcroFields;

                    // Set the field values here

                    stamper.FormFlattening = true;
                }

                pdfReader = new PdfReader(ms.ToArray());

                // Copy the memorystream to the main document
                copy.AddDocument(pdfReader);
            }
        }
    }

    document.CloseDocument();

    // Combine on A3 pages in new document

    var a3doc = new Document(PageSize.A3.Rotate(), 0, 0, 0, 0);

    var a3reader = new PdfReader(copyms.ToArray());
    var a3writer = PdfWriter.GetInstance(a3doc, new FileStream(outputFileLocation, FileMode.Create));

    a3doc.Open();

    var a3cb = a3writer.DirectContent;

    PdfImportedPage page;

    int totalPages = a3reader.NumberOfPages;

    for (int i = 1; i <= (int)Math.Ceiling(totalPages / 2); i++)
    {
        // Create an A3 page
        a3doc.NewPage();
        var a3size = PageSize.A3.Rotate();

        page = a3writer.GetImportedPage(a3reader, (i * 2) + 1);
        a3cb.AddTemplate(page, 0, 0);

        page = a3writer.GetImportedPage(a3reader, (i * 2) + 2);
        a3cb.AddTemplate(page, (int)(a3size.Width / 2), 0);                    
    }

    a3doc.CloseDocument();
}

So basically, keep everything in memory, and at the end use the writer's DirectContent to paste the A4 page somewhere on the A3 page. Also, use PdfSmartCopy to keep the file size low since the template page stuff isn't being added everytime you add a filled copy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Divide a A3 document in two A4 documents

From Dev

Print double sided and multiple pages on Ubuntu

From Dev

iTextSharp A4 size rounding

From Dev

Printing an A3 (Word) document on an A4 printer

From Dev

Crop and rearrange duplex scanned A3 pdf into A4 format

From Dev

Split/Tile A3 (landscape) pdf to A4 (portrait)

From Dev

Draw a line in the center of an A4 sheet iTextSharp

From Dev

Combining multiple sub-documents into a new doc in mongo

From Dev

duplex (double sided) print not available

From Dev

Merging documents with itextsharp

From Dev

Combining a list of documents in AQL?

From Dev

UIPrintInteractionController, turn off Double-sided option?

From Dev

THREE.JSONLoader - Double sided texture - PNG

From Dev

Collada. Double sided texture do not work

From Dev

Simple double sided custom shader looks strange

From Dev

Print Large SVG in multiple pages (A4)

From Dev

Print Large SVG in multiple pages (A4)

From Dev

Making a 4 sided Graph / 4 sided (Cartesian) grid In Visual Studio

From Dev

Splitting and combining of Double in Swift

From Dev

Splitting and combining of Double in Swift

From Dev

How can automatically print a page bigger than an A4 on multiple A4 pages?

From Dev

Which partition is better to use in a Nokia booklet 4g?

From Dev

iTextSharp multiple actions for pushbuttonfield

From Dev

Multiple layout events in iTextSharp

From Dev

I want to copy the Some of entire row having same word in the first column as in A3,A4,A5,A6 & paste to another sheet from the vacant rows?

From Dev

Three.js - Double sided plane one side reversed

From Dev

Issue with double-sided semi-transparent textures

From Dev

How do I scan a thousand double sided pages into a PDF?

From Dev

How can I print double-sided with ipptool?

Related Related

  1. 1

    Divide a A3 document in two A4 documents

  2. 2

    Print double sided and multiple pages on Ubuntu

  3. 3

    iTextSharp A4 size rounding

  4. 4

    Printing an A3 (Word) document on an A4 printer

  5. 5

    Crop and rearrange duplex scanned A3 pdf into A4 format

  6. 6

    Split/Tile A3 (landscape) pdf to A4 (portrait)

  7. 7

    Draw a line in the center of an A4 sheet iTextSharp

  8. 8

    Combining multiple sub-documents into a new doc in mongo

  9. 9

    duplex (double sided) print not available

  10. 10

    Merging documents with itextsharp

  11. 11

    Combining a list of documents in AQL?

  12. 12

    UIPrintInteractionController, turn off Double-sided option?

  13. 13

    THREE.JSONLoader - Double sided texture - PNG

  14. 14

    Collada. Double sided texture do not work

  15. 15

    Simple double sided custom shader looks strange

  16. 16

    Print Large SVG in multiple pages (A4)

  17. 17

    Print Large SVG in multiple pages (A4)

  18. 18

    Making a 4 sided Graph / 4 sided (Cartesian) grid In Visual Studio

  19. 19

    Splitting and combining of Double in Swift

  20. 20

    Splitting and combining of Double in Swift

  21. 21

    How can automatically print a page bigger than an A4 on multiple A4 pages?

  22. 22

    Which partition is better to use in a Nokia booklet 4g?

  23. 23

    iTextSharp multiple actions for pushbuttonfield

  24. 24

    Multiple layout events in iTextSharp

  25. 25

    I want to copy the Some of entire row having same word in the first column as in A3,A4,A5,A6 & paste to another sheet from the vacant rows?

  26. 26

    Three.js - Double sided plane one side reversed

  27. 27

    Issue with double-sided semi-transparent textures

  28. 28

    How do I scan a thousand double sided pages into a PDF?

  29. 29

    How can I print double-sided with ipptool?

HotTag

Archive