2017-01-19 41 views
0

我正在尝试合并PDF文档并为其中一些页面添加额外的页面。合并部分工作正常,现在我正试图弄清楚如何通过将链接传递给预先存在的PDF页面来添加额外的页面。如何将链接传递到PDF页面以targetDoc.AddPage(LINK)?使用PDFsharp库向PDF文档添加额外页面

public static void MergePDFs(string targetPath, DataTable pdfs) 
    { 
     try 
     { 
      using (PdfSharp.Pdf.PdfDocument targetDoc = new PdfSharp.Pdf.PdfDocument()) 
      { 
       foreach (DataRow pdf in pdfs.Rows) 
       { 
        using (PdfSharp.Pdf.PdfDocument pdfDoc = PdfSharp.Pdf.IO.PdfReader.Open(pdf["link"].ToString(), PdfDocumentOpenMode.Import)) 
        { 
         for (int i = 0; i < pdfDoc.PageCount; i++) 
         { 
          targetDoc.AddPage(pdfDoc.Pages[i]); 
         } 
        } 
       } 
       targetDoc.Save(targetPath); 
      } 
     } 
     catch(Exception ex) 
     { 
      Console.Write(ex); 
     } 
    } 

冲压方法

using (Stream pdfStream = new FileStream(sourceFileName, FileMode.Open)) 
{ 
using (Stream newpdfStream = new FileStream(newFileNameWithPath, FileMode.Create, FileAccess.ReadWrite)) 
{ 
    iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfStream); 
    PdfStamper pdfStamper = new PdfStamper(pdfReader, newpdfStream); 
    PdfContentByte pdfContentByte = pdfStamper.GetOverContent(pageNumber); 
    BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); 
    pdfContentByte.SetColorFill(BaseColor.RED); 
    pdfContentByte.SetFontAndSize(baseFont, 12); 
    pdfContentByte.BeginText(); 
    pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, inputText, Convert.ToInt32(xCoordinate), Convert.ToInt32(yCoordinate), 0); 
    pdfContentByte.EndText(); 
    pdfStamper.Close(); 
} 

}

+0

我不知道如果我的回答是,你在找什么。如果你想解释“通过一个链接到预先存在的pdf页面”意味着我可能不得不更新我的答案。 –

+0

谢谢。我正在尝试将PDF页面传递给我的打印方法,并在加盖后将其添加回来。我在 – user6934713

+0

之上加了我的冲压方法冲模使用iTextSharp。您可以使用iTextSharp将加盖的页面保存到流中,使用PDFsharp打开该页面并像使用其他页面一样使用加盖的页面。看起来你的代码已经将加盖的PDF保存在'newpdfStream'流中,你只需要打开PdfReader.Open() - 可以处理文件和流。 –

回答

2

要创建一个新的空白页面调用AddPage()没有参数。

targetDoc.AddPage(); 

您可能需要Clone()创建现有导入的页面(也添加(PdfPage))的多个副本:

targetDoc.AddPage((PdfPage)pdfDoc.Pages[i].Clone()); 
相关问题