我有创建网络应用程序的新任务(我将使用Asp.net + C#): •客户每天向我们提供X个PDF文件(x将每天不同) •我的应用程序需要获取此PDF文件并在每个PDF文件的第3页(不是每3页,紧接在第3页之后)插入空白页,然后将所有这些PDF文件连接成一个大的PDF文件。 我正在考虑使用Aspose,因为它似乎可以连接pdf文件,但我必须检查它是否也可以在pdf文件中插入页面。
是否还有其他任何插件,网络服务,代码或甚至是您知道的技术?
答案 0 :(得分: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。
免责声明:我是该图书馆的开发人员之一。
答案 1 :(得分:1)
我使用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;