大家好我怎样才能将多个PDF文件合并成一个大的PDF文件?我尝试了以下内容,但是这个错误显示“一个页面只能有一个服务器端的Form标签”我无法理解。这是我的代码。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnMerge_Click(object sender, EventArgs e)
{
if (file1.HasFile && file2.HasFile)
{
PdfReader pdfReader1 = new PdfReader(file1.PostedFile.InputStream);
PdfReader pdfReader2 = new PdfReader(file2.PostedFile.InputStream);
List<PdfReader> readerList = new List<PdfReader>();
readerList.Add(pdfReader1);
readerList.Add(pdfReader2);
//Define a new output document and its size, type
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
//Get instance response output stream to write output file.
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
foreach (PdfReader reader in readerList)
{
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
document.Add(iTextSharp.text.Image.GetInstance(page));
}
}
document.Close();
Response.AppendHeader("content-disposition", "inline; filename=OutPut.pdf");
Response.ContentType = "application/pdf";
}
}
private void MergePDFs(string outPutFilePath, params string[] filesPath)
{
List<PdfReader> readerList = new List<PdfReader>();
foreach (string filePath in filesPath)
{
PdfReader pdfReader = new PdfReader(filePath);
readerList.Add(pdfReader);
}
//Define a new output document and its size, type
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
//Create blank output pdf file and get the stream to write on it.
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outPutFilePath, FileMode.Create));
document.Open();
foreach (PdfReader reader in readerList)
{
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
document.Add(iTextSharp.text.Image.GetInstance(page));
}
}
document.Close();
}
}
}