我使用以下方法拍摄2张PDF(CADPDF& TemplatePDF,并将它们叠加为另一张pdf(FinalPDF)。
public void MergePDFs(string CADPDF, string TemplatePDF, string FinalPDF, string oldData, string newData, string jobNumber, string customer, string designer, string date)
{
PdfReader reader = new PdfReader(TemplatePDF);
PdfReader reader2 = new PdfReader(CADPDF);
PdfStamper stamper = new PdfStamper(reader, new FileStream(FinalPDF, FileMode.Append, FileAccess.Write));
PSModel psModel = powerSHAPE.ActiveModel;
try
{
AcroFields fields = stamper.AcroFields;
fields.SetField("oldDataField", oldData);
fields.SetField("newDataField", newData);
fields.SetField("jobNumberField", jobNumber);
fields.SetField("customerField", customer);
fields.SetField("dateField", date);
fields.SetField("designerField", designer);
stamper.FormFlattening = false;
PdfImportedPage page = stamper.GetImportedPage(reader2, 1);
PdfContentByte cb;
cb = stamper.GetOverContent(1);
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(1).Height);
stamper.Close();
}
catch
{
System.Windows.MessageBox.Show("Unable to merge drawing with template!");
}
stamper.Close();
}
一切都很好,但是如果这个FinalPDF已经存在,我现在需要追加另一个页面。我已将FileStream的FileMode更改为Append,但FinalPDF被覆盖而不是追加。
非常感谢任何方向,谢谢。
答案 0 :(得分:0)
通过使用内存流和byte []数组,iTextSharp获得了最佳结果。我使用PdfSmartCopy类生成一个空白PDF,然后从不同的源检索所需的元素作为byte []。
使用以下方法使用PdfStamper
返回数据的byte [] public byte[] GetContentToMerge(string CADPDF, string TemplatePDF, string oldData, string newData, string jobNumber, string customer, string designer, string date)
{
using (MemoryStream ms = new MemoryStream())
{
PdfReader reader = new PdfReader(TemplatePDF);
PdfReader reader2 = new PdfReader(CADPDF);
// Use a Memory stream instead of a filestream
PdfStamper stamper = new PdfStamper(reader, ms);
//PSModel psModel = powerSHAPE.ActiveModel;
try
{
AcroFields fields = stamper.AcroFields;
fields.SetField("oldDataField", oldData);
fields.SetField("newDataField", newData);
fields.SetField("jobNumberField", jobNumber);
fields.SetField("customerField", customer);
fields.SetField("dateField", date);
fields.SetField("designerField", designer);
stamper.FormFlattening = false;
PdfImportedPage page = stamper.GetImportedPage(reader2, 1);
PdfContentByte cb;
cb = stamper.GetOverContent(1);
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(1).Height);
stamper.Close();
reader.Close();
reader2.Close();
// The content you wanted to create is in a memory stream.
// It can bee appended to a PDF as a byte[]
byte[] ContentToAppend = ms.ToArray();
return ContentToAppend;
}
catch
{
System.Windows.MessageBox.Show("Unable to merge drawing with template!");
}
stamper.Close();
}
// If we made it here, something went wrong.
return new byte[0];
}
使用该方法,您可以从父方法调用它并以编程方式执行追加。
public void MergePDFs(string CADPDF, string TemplatePDF, string FinalPDF, string oldData, string newData, string jobNumber, string customer, string designer, string date)
{
using (Document oDoc = new Document())
using (MemoryStream ms = new MemoryStream())
{
PdfSmartCopy oPDF = new PdfSmartCopy(oDoc, ms);
oDoc.Open();
// If the FinalPDF file already exists, load it's data into the PDF 1st.
if (File.Exists(FinalPDF))
{
byte[] bytesFinalPDF = File.ReadAllBytes(FinalPDF);
oPDF.AddDocument(new PdfReader(bytesFinalPDF));
}
// If it doesn't exist, the oPDF is blank at this point.
// Second add the CAD and Template data to the PDF
// We do this by calling the GetContentToMerge method which returns the byte array of data that was created using the stamper.
byte[] ContentToAdd = GetContentToMerge(CADPDF, TemplatePDF, oldData, newData, jobNumber, customer, designer, date);
if (ContentToAdd.Length > 0)
{
oPDF.AddDocument(new PdfReader(ContentToAdd));
}
oDoc.Close();
// Take whatever went into oPDF object's memory stream and write it to a file with path FinalPDF
File.WriteAllBytes(FinalPDF, ms.ToArray());
}
}
**我没有100%调试这个,所以在检查文件是否存在,读/写权限等时可能需要一些清理......