PDFsharp用法:合并pdf文档

此示例显示如何从两个现有的PDF文件创建新文档。

从两个文档交替插入页面。这对于视觉比较可能很有用。

/// <summary>
/// Imports pages from an external document.
/// Note that this technique imports the whole page including the hyperlinks.
/// </summary>
static void Variant1()
{
  // Get two fresh copies of the sample PDF files
  // (Note: The input files are not modified in this sample.)
  string filename1 = "Portable Document Format.pdf";
  File.Copy(Path.Combine("../../../../PDFs/", filename1),
    Path.Combine(Directory.GetCurrentDirectory(), filename1), true);
  string filename2 = "Portable Document Format.pdf";  // use other file here
  File.Copy(Path.Combine("../../../../PDFs/", filename2),
    Path.Combine(Directory.GetCurrentDirectory(), filename2), true);
 
  // Open the input files
  PdfDocument inputDocument1 = PdfReader.Open(filename1, PdfDocumentOpenMode.Import);
  PdfDocument inputDocument2 = PdfReader.Open(filename2, PdfDocumentOpenMode.Import);
 
  // Create the output document
  PdfDocument outputDocument = new PdfDocument();
 
  // Show consecutive pages facing. Requires Acrobat 5 or higher.
  outputDocument.PageLayout = PdfPageLayout.TwoColumnLeft;
 
  XFont font = new XFont("Verdana", 10, XFontStyle.Bold);
  XStringFormat format = new XStringFormat();
  format.Alignment = XStringAlignment.Center;
  format.LineAlignment = XLineAlignment.Far;
  XGraphics gfx;
  XRect box;
  int count = Math.Max(inputDocument1.PageCount, inputDocument2.PageCount);
  for (int idx = 0; idx < count; idx++)
  {
    // Get page from 1st document
    PdfPage page1 = inputDocument1.PageCount > idx ?
      inputDocument1.Pages[idx] : new PdfPage();
 
    // Get page from 2nd document
    PdfPage page2 = inputDocument2.PageCount > idx ?
      inputDocument2.Pages[idx] : new PdfPage();
 
    // Add both pages to the output document
    page1 = outputDocument.AddPage(page1);
    page2 = outputDocument.AddPage(page2);
 
    // Write document file name and page number on each page
    gfx = XGraphics.FromPdfPage(page1);
    box = page1.MediaBox.ToXRect();
    box.Inflate(0, -10);
    gfx.DrawString(String.Format("{0} • {1}", filename1, idx + 1),
      font, XBrushes.Red, box, format);
 
    gfx = XGraphics.FromPdfPage(page2);
    box = page2.MediaBox.ToXRect();
    box.Inflate(0, -10);
    gfx.DrawString(String.Format("{0} • {1}", filename2, idx + 1),
      font, XBrushes.Red, box, format);
  }
 
  // Save the document...
  string filename = "CompareDocument1.pdf";
  outputDocument.Save(filename);
  // ...and start a viewer.
  Process.Start(filename);
}
 
/// <summary>
/// Imports the pages as form X objects.
/// Note that this technique copies only the visual content and the
/// hyperlinks do not work.
/// </summary>
static void Variant2()
{
  // Get fresh copies of the sample PDF files
  string filename1 = "Portable Document Format.pdf";
  File.Copy(Path.Combine("../../../../PDFs/", filename1),
    Path.Combine(Directory.GetCurrentDirectory(), filename1), true);
  string filename2 = "Portable Document Format.pdf";
  File.Copy(Path.Combine("../../../../PDFs/", filename2),
    Path.Combine(Directory.GetCurrentDirectory(), filename2), true);
 
  // Create the output document
  PdfDocument outputDocument = new PdfDocument();
 
  // Show consecutive pages facing
  outputDocument.PageLayout = PdfPageLayout.TwoPageLeft;
 
  XFont font = new XFont("Verdana", 10, XFontStyle.Bold);
  XStringFormat format = new XStringFormat();
  format.Alignment = XStringAlignment.Center;
  format.LineAlignment = XLineAlignment.Far;
  XGraphics gfx;
  XRect box;
 
  // Open the external documents as XPdfForm objects. Such objects are
  // treated like images. By default the first page of the document is
  // referenced by a new XPdfForm.
  XPdfForm form1 = XPdfForm.FromFile(filename1);
  XPdfForm form2 = XPdfForm.FromFile(filename2);
 
  int count = Math.Max(form1.PageCount, form2.PageCount);
  for (int idx = 0; idx < count; idx++)
  {
    // Add two new pages to the output document
    PdfPage page1 = outputDocument.AddPage();
    PdfPage page2 = outputDocument.AddPage();
 
    if (form1.PageCount > idx)
    {
      // Get a graphics object for page1
      gfx = XGraphics.FromPdfPage(page1);
 
      // Set page number (which is one-based)
      form1.PageNumber = idx + 1;
 
      // Draw the page identified by the page number like an image
      gfx.DrawImage(form1, new XRect(0, 0, form1.Width, form1.Height));
 
      // Write document file name and page number on each page
      box = page1.MediaBox.ToXRect();
      box.Inflate(0, -10);
      gfx.DrawString(String.Format("{0} • {1}", filename1, idx + 1),
        font, XBrushes.Red, box, format);
    }
 
    // Same as above for second page
    if (form2.PageCount > idx)
    {
      gfx = XGraphics.FromPdfPage(page2);
 
      form2.PageNumber = idx + 1;
      gfx.DrawImage(form2, new XRect(0, 0, form2.Width, form2.Height));
 
      box = page2.MediaBox.ToXRect();
      box.Inflate(0, -10);
      gfx.DrawString(String.Format("{0} • {1}", filename2, idx + 1),
        font, XBrushes.Red, box, format);
    }
  }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值