HOME > .NET Framework > Forum > สอบถามครับเขียน Merge PDF แต่พอเวลาไฟล์เยอะเจอ Error: CombineMultiplePDFs error:Rebuild failed: trailer not found.; Original message: PDF startxref not found.
สอบถามครับเขียน Merge PDF แต่พอเวลาไฟล์เยอะเจอ Error: CombineMultiplePDFs error:Rebuild failed: trailer not found.; Original message: PDF startxref not found.
สอบถามครับเขียน Merge PDF แต่พอเวลาไฟล์เยอะเจอ Error: CombineMultiplePDFs error:Rebuild failed: trailer not found.; Original message: PDF startxref not found.
หาทางแล้วแก้ไม่ถูกจุดสักทีครับ
code Code (C#)
public static void CombineMultiplePDFs(string[] fileNames, string outFile)
{
try
{
PdfReader reader = null;
PdfImportedPage importedPage;
// step 1: creation of a document-object
Document document = new Document();
// step 2: we create a writer that listens to the document
PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
if (writer == null)
{
return;
}
// step 3: we open the document
document.Open();
clsLog.Info("CombineMultiplePDFs outFile : " + outFile);
foreach (string fileName in fileNames)
{
clsLog.Info("Files :" + fileName);
// we create a reader for a certain document
if (File.Exists(fileName))
{
int pages = 0;
//PdfReader reader = new PdfReader(fileName);
//reader.ConsolidateNamedDestinations();
var its = new iTextSharp.text.pdf.PdfReader(fileName);
pages = its.NumberOfPages;
its.Close();
clsLog.Error("CombineMultiplePDFs In Exists : " + fileName);
// step 4: we add content
//for (int i = 1; i <= reader.NumberOfPages; i++)
//{
// clsLog.Error("CombineMultiplePDFs Loop : " + i);
// PdfImportedPage page = writer.GetImportedPage(reader, i);
// writer.AddPage(page);
//}
reader = new PdfReader(fileName);
//Add pages of current file
for (int i = 1; i <= pages; i++)
{
importedPage = writer.GetImportedPage(reader, i);
writer.AddPage(importedPage);
}
//PRAcroForm form = reader.AcroForm;
//if (form != null)
//{
// clsLog.Error("CombineMultiplePDFs form : null");
// writer.AddDocument(reader);
//}
reader.Close();
}
}
// step 5: we close the document and writer
writer.Close();
document.Close();
clsLog.Info("CombineMultiplePDFs End Completed");
}
catch (Exception ex)
{
clsLog.Error("CombineMultiplePDFs error:" + ex.Message);
throw new Exception("CombineMultiplePDFs error:" + ex.Message);
}
}