Home >Backend Development >C++ >How to Eliminate Excessive Whitespace When Merging PDFs?

How to Eliminate Excessive Whitespace When Merging PDFs?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-26 00:48:16944browse

How to Eliminate Excessive Whitespace When Merging PDFs?

Removing Whitespace on Merging

Problem:

When merging multiple PDFs, each page is considered a full page, even if it has only a small amount of content, resulting in substantial white space.

Solution:

private void Merge(MemoryStream outputStream, List<PdfReader> inputs) {
    try {
        this.OpenDocument(outputStream);

        foreach (PdfReader reader in inputs) {
            this.Merge(reader);
        } // foreach of the PDF files to merge
    } finally {
        this.CloseDocument();
    } // try-catch-finally
} // Merge

This code sets up the merging process by opening and initializing necessary structures.

public void Merge(PdfReader reader) {
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);

    for (int pageIndex = 1; pageIndex <= reader.NumberOfPages; pageIndex++) {
        this.Merge(reader, parser, pageIndex);
    } // foreach of the pages of the current PDF
} // Merge

Here, each page of the specified PdfReader is analyzed for merging.

public void Merge(PdfReader reader, PdfReaderContentParser parser, int pageIndex) {
    PdfImportedPage importedPage = Writer.GetImportedPage(reader, pageIndex);
    PdfContentByte directContent = Writer.DirectContent;

    PageVerticalAnalyzer finder = parser.ProcessContent(pageIndex, new PageVerticalAnalyzer());

    if (finder.VerticalFlips.Count < 2)
        return;

    Rectangle pageSizeToImport = reader.GetPageSize(pageIndex);

    int startFlip = finder.VerticalFlips.Count - 1;
    bool first = true;

    while (startFlip > 0) {
        if (!first)
            this.NewPage();

        float freeSpace = this.YPosition - PageSize.GetBottom(BottomMargin);
        int endFlip = startFlip + 1;

        while ((endFlip > 1) && (finder.VerticalFlips[startFlip] - finder.VerticalFlips[endFlip - 2] < freeSpace))
            endFlip -= 2;

        if (endFlip < startFlip) {
            float height = finder.VerticalFlips[startFlip] - finder.VerticalFlips[endFlip];

            directContent.SaveState();
            directContent.Rectangle(0, this.YPosition - height, pageSizeToImport.Width, height);
            directContent.Clip();
            directContent.NewPath();

            this.Writer.DirectContent.AddTemplate(importedPage, 0, this.YPosition - (finder.VerticalFlips[startFlip] - pageSizeToImport.Bottom));

            directContent.RestoreState();
            this.YPosition -= height + this.Gap;
            startFlip = endFlip - 1;
        } else if (!first) {
            throw new ArgumentException(string.Format("Page {0} content too large", pageIndex));
        } // if

        first = false;
    } // while
} // Merge

The Merge method then analyzes each page's vertical content using the PageVerticalAnalyzer class. It identifies vertical sections that can be merged, eliminating white space while preserving content.

The above is the detailed content of How to Eliminate Excessive Whitespace When Merging PDFs?. 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