【发布时间】:2012-03-03 11:05:56
【问题描述】:
我有一个模板 PDF 文件,其中嵌入了一个 PDF 表单字段。我正在使用 PdfStamper 填写这些字段。此外,我希望能够更改生成的 PDF 的边距。有什么方法可以修改加盖 PDF 的页边距吗?
【问题讨论】:
-
您是否需要保持现有页面大小与您的 PDF 模板相同,或者创建一个页面大小稍大/稍小的新文档是否可以接受?跨度>
标签: c# .net itextsharp
我有一个模板 PDF 文件,其中嵌入了一个 PDF 表单字段。我正在使用 PdfStamper 填写这些字段。此外,我希望能够更改生成的 PDF 的边距。有什么方法可以修改加盖 PDF 的页边距吗?
【问题讨论】:
标签: c# .net itextsharp
您可以在一行中完成所有操作。
Document doc = new Document(PageSize.LETTER, 0f, 0f, 0f, 0f );
【讨论】:
我只知道这样的方式。
iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(pageWidth, pageHeight);
Document doc = new Document(rec);
doc.SetMargins(0f, 0f, 0f, 0f);
但是,这也会限制边距
【讨论】:
setMaring Impelemented as
public override bool SetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
{
if ((this.writer != null) && this.writer.IsPaused())
{
return false;
}
this.nextMarginLeft = marginLeft;
this.nextMarginRight = marginRight;
this.nextMarginTop = marginTop;
this.nextMarginBottom = marginBottom;
return true;
}
因此为下一页申请了边距。 打开pdfDocument调用newPage()后解决这个问题 此解决方案适用于空的 pdfDocument。
using (FileStream msReport = new FileStream(pdfPath, FileMode.Create))
{
using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f))
{
try
{
//open the stream
pdfDoc.Open();
pdfDoc.setMargin(20f, 20f, 20f, 20f);
pdfDoc.NewPage();
pdfDoc.Close();
}
catch (Exception ex)
{
//handle exception
}
finally
{
}
}
}
【讨论】: