【问题标题】:Concatenate PDF files and insert a page into a PDF File连接 PDF 文件并将页面插入 PDF 文件
【发布时间】:2013-09-24 19:15:44
【问题描述】:

我得到了创建 Web 应用程序的新任务(我将使用 Asp.net + C#): • 客户每天给我们 X 个 PDF 文件(x 每天都会不同) • 我的应用程序需要获取此 PDF 文件,并在每个 PDF 文件的第 3 页(不是每 3 页,就在第 3 页之后)之后插入一个空白页,然后将所有这些 PDF 文件连接成一个大 PDF 文件。 我正在考虑使用 Aspose,因为它似乎可以连接 pdf 文件,但我必须检查它是否也可以在 pdf 文件中插入页面。

是否有任何其他插件、网络服务、背后的代码甚至是您知道的技术?

【问题讨论】:

  • iTextSharp 是一个很棒的开源工具。我在我们的环境中将它用于几乎完全相同的事情。具体来说,您应该查看PdfReaderPdfConcatenate 类。 Get it on Source Forge
  • 谢谢@Evanlewis ...我不喜欢iText的一点是他们没有很好的例子...您需要购买他们的书才能了解它可以做什么...甚至是简单的东西像拆分和连接一样,他们有一些奇怪的例子......
  • 没问题。我知道 iText 网站缺乏好的文档和示例,但 Stack Overflow 有很多很好的示例。用 iTextSharp 搜索 PDF Concatenate,你会看到很多很好的例子。不过,您需要对 C# 有相当好的理解才能正确使用该库。通常,您将使用输入/输出文件,并在拆分/合并文档后最终删除不需要的内容。有很多不错的 PDF 库,但 iText 功能绝对是我所知道的最好的免费选项。如果您想付款,请使用 Quick PDF。
  • 我不认为 Aspose 是免费的。

标签: c# asp.net pdf concatenation


【解决方案1】:

使用Docotic.Pdf library 可以轻松完成任务。

这是合并文件的代码,同时在每个文件的第三页之后添加空白页。

public static void insertBlanksAndMerge()
{
    string[] filesToMerge = { "file1.pdf", "file2.pdf" };
    
    // open first file
    int pagesBefore = 0;
    using (PdfDocument pdf = new PdfDocument(filesToMerge[0]))
    {
        pdf.InsertPage(pagesBefore + 3);

        // append all other documents
        for (int i = 1; i < filesToMerge.Length; i++)
        {
            pagesBefore = pdf.PageCount;

            pdf.Append(filesToMerge[i]);
            pdf.InsertPage(pagesBefore + 3);
        }

        pdf.Save(@"out.pdf");
    }
}

请注意PdfDocument 构造函数和Append 方法不仅可以使用文件名,还可以使用流和字节缓冲区。

更多样品可用on site

免责声明:我是该库的开发人员之一。

【讨论】:

  • 我可以免费将它用于免费软件应用程序吗?
  • @Foreever 不幸的是,没有。即使对于免费软件应用程序,该库也不再免费。
  • PdfDocument 不包含采用 1 个参数的构造函数。另外,PdfDocument 中不存在“InsertPage”...我使用了错误的参考吗?这不是 iTextSharp?
【解决方案2】:

我使用 iTextsharp 来合并 pdf 文件。这是我使用的代码。

string[] lstFiles=new string[3];
    lstFiles[0]=@"C:/pdf/1.pdf";
    lstFiles[1]=@"C:/pdf/2.pdf";
    lstFiles[2]=@"C:/pdf/3.pdf";

    PdfReader reader = null;
    Document sourceDocument = null;
    PdfCopy pdfCopyProvider = null;
    PdfImportedPage importedPage;
    string outputPdfPath=@"C:/pdf/new.pdf";


    sourceDocument = new Document();
    pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

    //Open the output file
    sourceDocument.Open();

    try
    {
        //Loop through the files list
        for (int f = 0; f < lstFiles.Length-1; f++)
        {
            int pages =get_pageCcount(lstFiles[f]);

            reader = new PdfReader(lstFiles[f]);
            //Add pages of current file
            for (int i = 1; i <= pages; i++)
            {
                importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                pdfCopyProvider.AddPage(importedPage);
            }

            reader.Close();
         }
        //At the end save the output file
        sourceDocument.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }


private int get_pageCcount(string file)
{
    using (StreamReader sr = new StreamReader(File.OpenRead(file)))
    {
        Regex regex = new Regex(@"/Type\s*/Page[^s]");
        MatchCollection matches = regex.Matches(sr.ReadToEnd());

        return matches.Count;
    }
}

编辑: 您需要的参考是

using iTextSharp.text;
using iTextSharp.text.pdf;

【讨论】:

  • 不要告诉我们我们可能需要 iTextSharp 中的哪些引用。
猜你喜欢
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
相关资源
最近更新 更多