@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.
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.
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).
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.
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.");
}
}
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). |
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!