【问题标题】:Adding white space in PDF using iTextSharp使用 iTextSharp 在 PDF 中添加空格
【发布时间】:2012-08-10 01:28:57
【问题描述】:

我有 PDF,其中一些文本非常靠近左右边框,我想使用 iTextSharp 为 PDF 中的每个页面在边框、左上角和右上角添加更多空白。

这可以使用 iTextSharp 还是有更好的方法?

【问题讨论】:

  • 您是在编辑现有的 PDF 文件,还是在创建新的 PDF 文件?

标签: c# itextsharp


【解决方案1】:

我设法找到了答案。以下代码调整页面大小:

var inputPdf = new PdfReader(inputFile);   // Get input document

int pageCount = inputPdf.NumberOfPages;

if (end < start || end > pageCount)
    end = pageCount;

var inputDoc = new Document(inputPdf.GetPageSizeWithRotation(1)); 

using (var fs = new FileStream(outputFile, FileMode.Create))
{
    var outputWriter = PdfWriter.GetInstance(inputDoc, fs);
    inputDoc.Open();

    PdfContentByte cb1 = outputWriter.DirectContent;

    // Copy pages from input to output document
    for (int i = start; i <= end; i++)
    {
        var existingRec = inputPdf.GetPageSizeWithRotation(i);
        var newRec = new Rectangle(0.0f, 0.0f, existingRec.Width + 50, existingRec.Height + 25, 0);

        inputDoc.SetPageSize(newRec);
        inputDoc.NewPage();

        PdfImportedPage page = outputWriter.GetImportedPage(inputPdf, i);
        int rotation = inputPdf.GetPageRotation(i);

        if (rotation == 90 || rotation == 270)
             cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, inputPdf.GetPageSizeWithRotation(i).Height);
        else cb1.AddTemplate(page, 1f, 0, 0, 1f, 25, 13); 
    }

    inputDoc.Close();
}

希望这对某人有所帮助。

【讨论】:

  • 什么是结束和开始?
  • @CountLessQ public static void AddWhiteSpace(string inputFile, string outputFile, int start, int end) 其中 start 和 end 是您要添加空白的页数,例如第 1 页到第 3 页。
猜你喜欢
  • 2011-01-31
  • 2013-02-09
  • 2015-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多