【问题标题】:ITextSharp - merge two pdfs in a single pageITextSharp - 在一个页面中合并两个 pdf
【发布时间】:2011-05-04 07:20:09
【问题描述】:

我会把这个问题简单地说出来。

我有这个 pdf:

 _____
|abcd |
|     |
|     |
|_____|

还有这个:

 _____
|1234 |
|4567 |
|     |
|_____|

我想将它们合并得到:

 _____
|abcd |
|1234 |
|4567 |
|_____|

是否可以使用 iTextSharp 或任何其他免费工具?

提前致谢

【问题讨论】:

    标签: c# asp.net pdf itextsharp


    【解决方案1】:

    这是一个老问题......但如果有人再次进入这里,我的解决方案是...... 我把两页硬编码成一页所以这是基础 首先我旋转了两个 PDF,然后将它们合并在一起

    旋转两个页面使用这个:

     public static void RotatePDF(string inputFile, string outputFile)
        {
            using (FileStream outStream = new FileStream(outputFile, FileMode.Create))
            {
                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(inputFile);
                iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outStream);
    
                iTextSharp.text.pdf.PdfDictionary pageDict = reader.GetPageN(1);
                int desiredRot = 90; // 90 degrees clockwise from what it is now
                iTextSharp.text.pdf.PdfNumber rotation = pageDict.GetAsNumber(iTextSharp.text.pdf.PdfName.ROTATE);
    
                if (rotation != null)
                {
                    desiredRot += rotation.IntValue;
                    desiredRot %= 360; // must be 0, 90, 180, or 270
                }
                pageDict.Put(iTextSharp.text.pdf.PdfName.ROTATE, new iTextSharp.text.pdf.PdfNumber(desiredRot));
    
                stamper.Close();
            }
        }
    

    现在您可以将它们合并在一起:

            public static void MergeTwoPdfsToSingle(string inputFile1, string inputFile2, string outputFile)
        {
            //Step 1: Create a Docuement-Object
            Document document = new Document();
            try
            {
                //Step 2: we create a writer that listens to the document
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFile, FileMode.Create));
    
                //Step 3: Open the document
                document.Open();
    
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage page1;
                PdfImportedPage page2;                
    
                // we create a reader for the document
                PdfReader reader1 = new PdfReader(inputFile1);
                PdfReader reader2 = new PdfReader(inputFile2);
    
                document.SetPageSize(reader1.GetPageSizeWithRotation(1));
                document.NewPage();
    
                page1 = writer.GetImportedPage(reader1, 1);                                
    
                page2 = writer.GetImportedPage(reader2, 1);                
    
                cb.AddTemplate(page1, 0, 0);
                //play around to find the exact location for the next pdf
                cb.AddTemplate(page2, 0, 300);
            }
            catch (Exception e) { throw e; }
            finally { document.Close(); }
        }
    

    【讨论】:

    • 您是否知道如何避免丢失数据,例如 Adob​​e Acrobat DC 表单/控件?测试了您的代码,它适用于 PDF 的图形和文本,但我失去了文本框、组合框和其他可编辑的 Adob​​e Acrobat DC 控件等控件。
    • @PaulWeiland - 从我在其他线程上阅读的内容来看,如果您想在复制到新 PDF 时保留控件,那么您需要使用 PDFCopy 类。
    【解决方案2】:

    是的...这只是非常困难,即使对于 PDF 专家也是如此。通过提出这个问题,你已经表明你不是一个……至少现在还不是。把它关掉,你会顺利上路的……但是:

    没有简单的方法来确定包围给定页面上所有内容的边界框。 com.itextpdf.text.pdf.parser(或它的 # 等价物)有几个类可能会对您有所帮助,但最重要的是,PDF 并非旨在像这样解析。

    强烈建议您尝试其他方法。任何涉及“然后我们从 PDF 中获取信息”这一短语的内容都需要进行彻底检查。哦,这是可能的,但几乎总是有更好的方法来做到这一点。

    【讨论】:

      【解决方案3】:

      我们使用了一个名为 PDFMerger 的产品,它可以做到这一点。然而它并不便宜。我们真的没有找到其他可以轻松完成此任务的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-13
        • 1970-01-01
        • 2017-10-23
        • 2019-10-30
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        • 2012-09-27
        相关资源
        最近更新 更多