使用iTextSharp在PDF中添加空白
问题描述:
我有PDF,其中一些文本非常靠近左侧和右侧边框,我希望在边框处添加更多的空白区域,左下角和右下角,为每个页面使用iTextSharp的PDF。使用iTextSharp在PDF中添加空白
这可能使用iTextSharp还是有更好的方法?
答
我设法找到了答案,以下是调整页面大小:
// get input document
PdfReader inputPdf = new PdfReader(inputFile);
// retrieve the total number of pages
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount)
{
end = pageCount;
}
// load the input document
Document inputDoc =
new Document(inputPdf.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
// create the output writer
PdfWriter 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++)
{
Rectangle existingRec = inputPdf.GetPageSizeWithRotation(i);
//new Rectangle(0.0f, 0.0f, 757.0f, 1087.0f, 0);
//
//newRec.Height = 1577;
Rectangle 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();
}
你编辑现有的PDF文件,或创建一个新的PDF文件? – Stewbob 2012-08-10 16:15:35