C# 使用 iTextSharp 设置页边距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9236931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set page margins with iTextSharp
提问by Tyler Schroeder
I have a template PDF file that has a a PDF form field embedded in. I am using PdfStamper to fill out these fields. In addition, I would like to be able to change the margins for generated PDF. is there any way I can modify the page margins on the stamped PDF?
我有一个模板 PDF 文件,其中嵌入了一个 PDF 表单字段。我正在使用 PdfStamper 来填写这些字段。此外,我希望能够更改生成的 PDF 的边距。有什么方法可以修改加盖 PDF 的页边距吗?
回答by Lukas
Only way I know of is like this.
我所知道的唯一方法是这样。
iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(pageWidth, pageHeight);
Document doc = new Document(rec);
doc.SetMargins(0f, 0f, 0f, 0f);
However, this will limit margins too
但是,这也会限制利润率
回答by Baxter
You can do it all in one line.
您可以在一行中完成所有操作。
Document doc = new Document(PageSize.LETTER, 0f, 0f, 0f, 0f );
回答by user934335
setMaring Impelemented as
setMaring 引入为
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;
}
therefor margin applied for next page. for solve this problem after open pdfDocument call newPage() this solution work for empty pdfDocument .
因此为下一页申请了保证金。为了在打开 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
{
}
}
}

