Home >Backend Development >C++ >How to Eliminate Excess Whitespace When Merging PDFs in C#?
Problem:
When merging multiple PDF documents, each page is considered a full page even if there's only a small amount of content, resulting in vertical whitespace in the merged document.
Solution:
public class PdfVeryDenseMergeTool { // ... (rest of the class) protected Rectangle PageSize; protected float TopMargin; protected float BottomMargin; protected float Gap; protected Document Document; protected PdfWriter Writer; protected float YPosition = 0; public PdfVeryDenseMergeTool(Rectangle size, float top, float bottom, float gap) { // ... (rest of the constructor) } public void Merge(MemoryStream outputStream, List<PdfReader> inputs) { // ... (rest of the method) } }
public class PageVerticalAnalyzer : IRenderListener { // ... (rest of the class) public List<float> VerticalFlips = new List<float>(); // ... (rest of the methods) }
public void TestMergeDocuments() { PdfVeryDenseMergeTool tool = new PdfVeryDenseMergeTool(iTextSharp.text.PageSize.A4, 18, 18, 10); List<byte[]> Files = new List<byte[]>(); // ... (code to load files) using (MemoryStream ms = new MemoryStream()) { List<PdfReader> files = new List<PdfReader>(); foreach (byte[] ba in Files) { files.Add(new PdfReader(ba)); } tool.Merge(ms, files); // ... (save the file using ms.GetBuffer()) } }
The above is the detailed content of How to Eliminate Excess Whitespace When Merging PDFs in C#?. For more information, please follow other related articles on the PHP Chinese website!