【发布时间】:2012-08-10 01:28:57
【问题描述】:
我有 PDF,其中一些文本非常靠近左右边框,我想使用 iTextSharp 为 PDF 中的每个页面在边框、左上角和右上角添加更多空白。
这可以使用 iTextSharp 还是有更好的方法?
【问题讨论】:
-
您是在编辑现有的 PDF 文件,还是在创建新的 PDF 文件?
标签: c# itextsharp
我有 PDF,其中一些文本非常靠近左右边框,我想使用 iTextSharp 为 PDF 中的每个页面在边框、左上角和右上角添加更多空白。
这可以使用 iTextSharp 还是有更好的方法?
【问题讨论】:
标签: c# itextsharp
我设法找到了答案。以下代码调整页面大小:
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();
}
希望这对某人有所帮助。
【讨论】: