首页 >后端开发 >C++ >如何在C#中合并PDF文件时消除垂直空格?

如何在C#中合并PDF文件时消除垂直空格?

Susan Sarandon
Susan Sarandon原创
2025-01-05 00:00:40785浏览

How to Eliminate Vertical Whitespace When Merging PDF Files in C#?

如何删除合并时的空格

此代码有效地合并了三个 PDF 字节数组。但是,生成的文档在每个页面上都显示垂直空白,使其显示为单独的页面。以下是消除此空格的方法:

C# 实现(已更新)

public class PdfVeryDenseMergeTool
{
    private Rectangle PageSize;
    private float TopMargin;
    private float BottomMargin;
    private float Gap;
    private Document Document = null;
    private PdfWriter Writer = null;
    private float YPosition = 0;

    public PdfVeryDenseMergeTool(Rectangle size, float top, float bottom, float gap)
    {
        this.PageSize = size;
        this.TopMargin = top;
        this.BottomMargin = bottom;
        this.Gap = gap;
    } // PdfVeryDenseMergeTool

    public 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

    public void OpenDocument(MemoryStream outputStream)
    {
        this.Document = new Document(PageSize, 36, 36, this.TopMargin, this.BottomMargin);
        this.Writer = PdfWriter.GetInstance(Document, outputStream);

        this.Document.Open();
        this.NewPage();
    } // OpenDocument

    public void CloseDocument()
    {
        try
        {
            this.Document.Close();
        }
        finally
        {
            this.Document = null;
            this.Writer = null;
            this.YPosition = 0;
        } // try-finally
    } // CloseDocument

    public void NewPage()
    {
        this.Document.NewPage();
        this.YPosition = PageSize.GetTop(this.TopMargin);
    } // Merge

    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

    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) &amp;&amp; (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
} // PdfVeryDenseMergeTool

PageVerticalAnalyzer 类:

public class PageVerticalAnalyzer : IRenderListener
{
    public PageVerticalAnalyzer() { }

    public List<float> VerticalFlips = new List<float>();

    public void AddVerticalUseSection(float from, float to)
    {
        if (to < from)
        {
            float temp = to;
            to = from;
            from = temp;
        }

        int i = 0;
        int j = 0;

        for (i = 0; i < VerticalFlips.Count; i++)
        {
            float flip = VerticalFlips[i];
            if (flip < from)
                continue;

            for (j = i; j < VerticalFlips.Count; j++)
            {
                flip = VerticalFlips[j];
                if (flip < to)
                    continue;
                break;
            }
            break;
        } // foreach of the vertical flips

        bool fromOutsideInterval = i % 2 == 0;
        bool toOutsideInterval = j % 2 == 0;

        while (j-- > i)
            VerticalFlips.RemoveAt(j); // This was the problem line with just .Remove(j)
        if (toOutsideInterval)

以上是如何在C#中合并PDF文件时消除垂直空格?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn