C# 如果使用母版存在PDF,请单击“附加”页
我使用以下方法获取两个pdf(CADPDF和TemplatePDF),并将它们叠加为另一个pdf(FinalPDF) 一切都很好,但是现在如果这个FinalPDF已经存在,我需要附加另一个页面。我已经将FileStream的FileMode更改为append,但是FinalPDF被覆盖而不是addedC# 如果使用母版存在PDF,请单击“附加”页,c#,itext,C#,Itext,我使用以下方法获取两个pdf(CADPDF和TemplatePDF),并将它们叠加为另一个pdf(FinalPDF) 一切都很好,但是现在如果这个FinalPDF已经存在,我需要附加另一个页面。我已经将FileStream的FileMode更改为append,但是FinalPDF被覆盖而不是added 非常感谢您的指导。我使用内存流和字节[]数组在iTextSharp中获得了最佳效果。我使用PdfSmartCopy类生成了一个空白PDF,然后从不同的源中检索所需的元素作为字节[] 使用以下方法使
非常感谢您的指导。我使用内存流和字节[]数组在iTextSharp中获得了最佳效果。我使用PdfSmartCopy类生成了一个空白PDF,然后从不同的源中检索所需的元素作为字节[] 使用以下方法使用PdfStamper返回数据的字节[]
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%调试此文件,因此可能需要对检查文件是否存在、读/写权限等进行一些清理。
FileMode.Append
当然是错误的。iText的输出始终是整个结果文件。只要结果位置上的文件长度超过零,就会产生FileMode.Append
无效PDF中的t(无论Adobe Reader是否识别它,这是另一回事)。这看起来正是我需要的。非常感谢!
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());
}
}