【问题标题】:Set page margins with iTextSharp使用 iTextSharp 设置页边距
【发布时间】:2012-03-03 11:05:56
【问题描述】:

我有一个模板 PDF 文件,其中嵌入了一个 PDF 表单字段。我正在使用 PdfStamper 填写这些字段。此外,我希望能够更改生成的 PDF 的边距。有什么方法可以修改加盖 PDF 的页边距吗?

【问题讨论】:

  • 您是否需要保持现有页面大小与您的 PDF 模板相同,或者创建一个页面大小稍大/稍小的新文档是否可以接受?跨度>

标签: c# .net itextsharp


【解决方案1】:

您可以在一行中完成所有操作。

Document doc = new Document(PageSize.LETTER, 0f, 0f, 0f, 0f );

【讨论】:

    【解决方案2】:

    我只知道这样的方式。

    iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(pageWidth, pageHeight); 
    Document doc = new Document(rec); 
    doc.SetMargins(0f, 0f, 0f, 0f);
    

    但是,这也会限制边距

    【讨论】:

    • 非常感谢!那是一个烦人的问题。还可能需要注意 pageWidth 和 pageHeight 以像素为单位。我使用 612 x 792(对于 72dpi)来获得常规的页面大小。
    【解决方案3】:

    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
                        {
    
    
                        }
    
                    }
    
                }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      相关资源
      最近更新 更多