Seamlessly combine DOCX pages vertically into one PNG

Does Aspose.Words support exporting a multi-page DOCX into one seamless PNG (all pages appended vertically without gaps)?

Sample

Expected
A single long PNG with no white space or margins between pages (true seamless vertical merge).

Notes

  • Using GDI+ renderer → .NET Framework build required
  • Tried MultiPageLayout.Vertical(0), but still see white gaps

Environment

  • Aspose.Words 25.8.0
  • Aspose.Words.Shaping.Harfbuzz 25.8.0
  • .NET Framework 4.8

@SzuChiChen

Yes – you can render a multi‑page DOCX as one seamless vertical PNG with Aspose.Words

The gaps you see are the page margins that Word adds to each page.
If you tell Aspose.Words to ignore those margins and use the Vertical mult‑page layout, the pages will be concatenated directly one after another.

Below is a complete, ready‑to‑run example for .NET Framework 4.8 (Aspose.Words 25.8.0) that produces a single PNG without any white space.


:one: Prepare the document (remove margins)

using Aspose.Words;
using Aspose.Words.Saving;

// Load the source DOCX
var doc = new Document(@"C:\Docs\multi-page.docx");

// Set every section’s margins to 0 (or any value you prefer)
foreach (Section sec in doc.Sections)
{
    sec.PageSetup.TopMargin = 0;
    sec.PageSetup.BottomMargin = 0;
    sec.PageSetup.LeftMargin = 0;
    sec.PageSetup.RightMargin = 0;
}

Why?
The renderer respects the section page‑setup margins. Setting them to 0 removes the blank stripe that otherwise appears between pages.


:two: Configure the image‑save options

// Create PNG‑specific options
var imgOptions = new ImageSaveOptions(SaveFormat.Png)
{
    // Render all pages in a single image
    PageCount = 0,                     // 0 = all pages
    // Vertical concatenation (top‑to‑bottom)
    MultiPageLayout = MultiPageLayout.Vertical,
    // Optional – make the background transparent
    TransparentBackground = true,
    
    // If you want to control the DPI, uncomment:
    // Resolution = 300
};

Key properties

  • MultiPageLayout = MultiPageLayout.Vertical – tells Aspose.Words to stack pages vertically.
  • PageCount = 0 – processes all pages.
  • TransparentBackground = true – removes the default white canvas (useful when the document has no background colour).

:three: Save the single PNG

// Save the combined image
doc.Save(@"C:\Output\merged.png", imgOptions);

That’s it – merged.png will contain the 13 pages of the sample document one underneath the other, with no white gaps.


:four: Full sample (copy‑paste)

using System;
using Aspose.Words;
using Aspose.Words.Saving;

class Program
{
    static void Main()
    {
        // 1️⃣ Load and strip margins
        var doc = new Document(@"C:\Docs\multi-page.docx");
        foreach (Section sec in doc.Sections)
        {
            sec.PageSetup.TopMargin = 0;
            sec.PageSetup.BottomMargin = 0;
            sec.PageSetup.LeftMargin = 0;
            sec.PageSetup.RightMargin = 0;
        }

        // 2️⃣ Set image‑save options
        var imgOptions = new ImageSaveOptions(SaveFormat.Png)
        {
            PageCount = 0,                      // all pages
            MultiPageLayout = MultiPageLayout.Vertical,
            TransparentBackground = true,
            //Resolution = 300                 // uncomment if you need higher DPI
        };

        // 3️⃣ Save as a single PNG
        doc.Save(@"C:\Output\merged.png", imgOptions);
        Console.WriteLine("Done – merged image saved.");
    }
}

:five: Common pitfalls & how to avoid them

Symptom Cause Fix
White strip between pages Section margins not set to 0. Set TopMargin/BottomMargin (or use Document.PageSetup) to 0 for every section.
Background appears white Default page colour is white. Set TransparentBackground = true (or PaperColor = Color.Empty).
Image is too small/blurred Low DPI. Increase Resolution in ImageSaveOptions (e.g., 300 dpi).
Only first page is rendered PageCount left at default (1). Set PageCount = 0 (or a specific number > 1).

:six: References

If you run into any issues (e.g., custom section breaks, different paper sizes, or you need to trim a specific amount of whitespace), let me know the exact document characteristics and I’ll provide a targeted snippet. Happy coding!

I’m exporting a multi-page DOCX to one PNG using MultiPageLayout.Vertical(0). Even after setting all section margins to 0, I still see gaps between pages—especially when the content flows inside a table.

Exmaple docx:
multi_page_English.docx (15.1 KB)
multi_page_Chinese.docx (13.8 KB)

Aspose Output (png):
multi_page_English


multi_page_Chinese

Example Code:

var license = new License();
license.SetLicense(@"licensePath");

var document = new Document(@"input.docx")
{
    LayoutOptions =
    {
        TextShaperFactory = HarfBuzzTextShaperFactory.Instance
    }
};
foreach (var node in document.Sections)
{
    var sec = (Section)node;
    sec.PageSetup.LeftMargin = 0;
    sec.PageSetup.RightMargin = 0;
    sec.PageSetup.TopMargin = 0;
    sec.PageSetup.BottomMargin = 0;
}
var saveOptions = new ImageSaveOptions(SaveFormat.Png)
{
    PageLayout = MultiPageLayout.Vertical(0),
};

document.Save(@"output.png", saveOptions);

@SzuChiChen The problem occurs because content in your document is in table. You can see this by setting top and bottom margins to zero in MS Word. Probably, you can increase page height before rendering the document to get the expected output. Something like this:

Document doc = new Document("C:\\Temp\\in.docx");

PageSetup pageSetup = doc.FirstSection.PageSetup;
double pageContentHeight = pageSetup.PageHeight - pageSetup.TopMargin - pageSetup.BottomMargin;

// Check whether document fits one page. If not increase page height.
while (doc.PageCount > 1)
{
    double addition = doc.PageCount > 2 ? (doc.PageCount - 1) * pageContentHeight : 10;
    pageSetup.PageHeight = pageSetup.PageHeight + addition;
    doc.UpdatePageLayout();
}

doc.Save("C:\\Temp\\out.png");

out.png (7.0 KB)

1 Like

@alexey.noskov I really appreciate your support. The solution worked perfectly!

1 Like