Home >Backend Development >C++ >How to Eliminate Excess Whitespace When Merging PDFs in C#?

How to Eliminate Excess Whitespace When Merging PDFs in C#?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-29 03:55:10336browse

How to Eliminate Excess Whitespace When Merging PDFs in C#?

How to Remove Whitespace on Merge

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:

Using the PdfVeryDenseMergeTool Class (C#):

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)
    }
}

Using the PageVerticalAnalyzer Class (C#):

public class PageVerticalAnalyzer : IRenderListener
{
    // ... (rest of the class)
    public List<float> VerticalFlips = new List<float>();

    // ... (rest of the methods)
}

Code to Gather Files and Run the Tool (C#):

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn