PDFsharp用法:两页一页

此示例显示如何将现有文档的两页放在新文档的一个横向页面上。

// Get a fresh copy of the sample PDF file
string filename = "Portable Document Format.pdf";
File.Copy(Path.Combine("../../../../PDFs/", filename), 
  Path.Combine(Directory.GetCurrentDirectory(), filename), true);
 
// Create the output document
PdfDocument outputDocument = new PdfDocument();
 
// Show single pages
// (Note: one page contains two pages from the source document)
outputDocument.PageLayout = PdfPageLayout.SinglePage;
 
XFont font = new XFont("Verdana", 8, XFontStyle.Bold);
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Center;
format.LineAlignment = XLineAlignment.Far;
XGraphics gfx;
XRect box;
 
// Open the external document as XPdfForm object
XPdfForm form = XPdfForm.FromFile(filename);
 
for (int idx = 0; idx < form.PageCount; idx += 2)
{
  // Add a new page to the output document
  PdfPage page = outputDocument.AddPage();
  page.Orientation = PageOrientation.Landscape;
  double width  = page.Width;
  double height = page.Height;
 
  gfx = XGraphics.FromPdfPage(page);
 
  // Set page number (which is one-based)
  form.PageNumber = idx + 1;
 
  box = new XRect(0, 0, width / 2, height);
  // Draw the page identified by the page number like an image
  gfx.DrawImage(form, box);
 
  // Write document file name and page number on each page
  box.Inflate(0, -10);
  gfx.DrawString(String.Format("- {1} -", filename, idx + 1),
    font, XBrushes.Red, box, format);
 
  if (idx + 1 < form.PageCount)
  {
    // Set page number (which is one-based)
    form.PageNumber = idx + 2;
 
    box = new XRect(width / 2, 0, width / 2, height);
    // Draw the page identified by the page number like an image
    gfx.DrawImage(form, box);
 
    // Write document file name and page number on each page
    box.Inflate(0, -10);
    gfx.DrawString(String.Format("- {1} -", filename, idx + 2),
      font, XBrushes.Red, box, format);
  }
}
 
// Save output document
outputDocument.Save("TwoOnOne.pdf");
 
// Start the viewer
Process.Start("TwoOnOne.pdf");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用pdfsharp删除PDF文本的示例代码: ```c# using PdfSharp.Pdf; using PdfSharp.Pdf.Content; using PdfSharp.Pdf.Content.Objects; using PdfSharp.Pdf.IO; public void DeleteTextFromPdf(string inputFilePath, string outputFilePath, string textToDelete) { // Load the PDF file PdfDocument document = PdfReader.Open(inputFilePath, PdfDocumentOpenMode.Modify); // Iterate through each page of the PDF foreach (PdfPage page in document.Pages) { // Extract the content of the page CObject[] content = ContentReader.ReadContent(page); // Iterate through each content object for (int i = content.Length - 1; i >= 0; i--) { COperator @operator = content[i] as COperator; if (@operator != null && @operator.OpCode.Name == "Tj") { // This is a text object, get the text CString text = content[i - 1] as CString; if (text != null && text.Value.Contains(textToDelete)) { // This text object contains the text to delete, remove it content[i - 1] = new CComment(); content[i] = new CComment(); } } } // Replace the page content with the modified content page.Contents.Clear(); page.Contents.AddRange(content); } // Save the modified PDF to a new file document.Save(outputFilePath); } ``` 在这个示例中,我们首先加载了输入PDF文件。我们然后遍历每一页,并提取页面的内容。我们接着遍历每个内容对象,并查找文本对象。如果找到了包含要删除的文本的文本对象,我们将其替换为注释对象。最后,我们用修改后的内容替换页面的内容,并将修改后的PDF文件保存到输出文件中。 要使用此方法,只需将要删除的文本传递给`textToDelete`参数,然后将输入PDF文件的路径传递给`inputFilePath`参数,将输出PDF文件的路径传递给`outputFilePath`参数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值