Welcome Guest! To enable all features please Login or Register.



Go to last post Go to first unread
Paul Rayman  
#1 Posted : Friday, February 5, 2021 7:14:53 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,094

Thanks: 7 times
Was thanked: 128 time(s) in 125 post(s)
How to combine the content of two pages into one.
In this way for example:

Source document:


Here is the final code that illustrates the movement of both page content and annotations.
There is only a nuance, it works on documents with an even number of pages.


using (var srcDoc = PdfDocument.Load(@"e:\10\sample.pdf"))
    using (var dstDoc = PdfDocument.CreateNew())
        for (int i = 0; i < srcDoc.Pages.Count; i += 2)
            //Add a new page to the end of the target document. 
            //Page width is the sum of the widths of the imported pages. 
            //Height is the maximum height of imported pages.
            var combinedPage = dstDoc.Pages.InsertPageAt(dstDoc.Pages.Count, srcDoc.Pages[i].Width + srcDoc.Pages[i + 1].Width, Math.Max(srcDoc.Pages[i].Height, srcDoc.Pages[i + 1].Height));
            //Import pages i and i + 1 from source document to target document at position 0 and 1, respectively.
            dstDoc.Pages.ImportPages(srcDoc, $"{i + 1},{i + 2}", 0);
            //Move page content from the imported page at position 0 to the newly created page without offset.
            MovePageContent(dstDoc.Pages[0], combinedPage, 0, 0);
            //Move page content from the imported page at position 1 to the newly created page, offset by the width of the original page. 
            MovePageContent(dstDoc.Pages[1], combinedPage, dstDoc.Pages[1].Width, 0);
            //Generate content of newly created page.
            //Move page annotations
            MovePageAnnotations(dstDoc.Pages[0], combinedPage, 0, 0);
            MovePageAnnotations(dstDoc.Pages[1], combinedPage, dstDoc.Pages[1].Width, 0);
            //delete imported pages
        dstDoc.Save(@"e:\10\test.pdf", SaveFlags.NoIncremental);

private static void MovePageAnnotations(PdfPage from, PdfPage to, float offsetX, float offsetY)
    if (from.Annots == null || from.Annots.Count == 0)
    foreach (var annot in from.Annots)
        var dict = annot.Dictionary.Clone() as PdfTypeDictionary;
        if (dict.ContainsKey("P"))
        var newAnnot = PdfAnnotation.Create(dict, to);
        if (offsetX > 0.00001 || offsetX < -0.00001 || offsetY > 0.00001 || offsetY < -0.00001)
            var r = newAnnot.Rectangle;
            r.left += offsetX;
            r.right += offsetX;
            r.top += offsetY;
            r.bottom += offsetY;
            newAnnot.Rectangle = r;
        //foreach (var obj in newAnnot.NormalAppearance)
        //   TranslateObject(obj, offsetX, offsetY);

private static void MovePageContent(PdfPage from, PdfPage to, float offsetX, float offsetY)
    foreach (var obj in from.PageObjects)
        var clonedObj = obj.Clone();
        if (offsetX > 0.00001 || offsetX < -0.00001 || offsetY > 0.00001 || offsetY < -0.00001)
            TranslateObject(clonedObj, offsetX, offsetY);

private static void TranslateObject(PdfPageObject clonedObj, float offsetX, float offsetY)
    clonedObj.TransformClipPath(new FS_MATRIX(1, 0, 0, 1, offsetX, offsetY));
    if (clonedObj is PdfTextObject)
        var m = (clonedObj as PdfTextObject).Matrix;
        m.Translate(offsetX, offsetY);
        (clonedObj as PdfTextObject).Matrix = m;
    else if (clonedObj is PdfPathObject)
        var m = (clonedObj as PdfPathObject).Matrix;
        m.Translate(offsetX, offsetY);
        (clonedObj as PdfPathObject).Matrix = m;
    else if (clonedObj is PdfImageObject)
        var m = (clonedObj as PdfImageObject).Matrix;
        m.Translate(offsetX, offsetY);
        (clonedObj as PdfImageObject).Matrix = m;
    else if (clonedObj is PdfShadingObject)
        var m = (clonedObj as PdfShadingObject).Matrix;
        m.Translate(offsetX, offsetY);
        (clonedObj as PdfShadingObject).Matrix = m;
    else if (clonedObj is PdfFormObject)
        var m = (clonedObj as PdfFormObject).Matrix;
        m.Translate(offsetX, offsetY);
        (clonedObj as PdfFormObject).Matrix = m;

Edited by user Monday, March 1, 2021 4:21:04 PM(UTC)  | Reason: Not specified

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.