【问题标题】:create PDF file using itextsharp in C#在 C# 中使用 itextsharp 创建 PDF 文件
【发布时间】:2013-02-22 14:16:34
【问题描述】:

我需要创建 PDF 文件,其中使用左侧、右侧和顶部的边距在页面上画线。但是在这里,由于这个原因造成的混乱,这些边距的计算是以像素值计算的。那么,如何通过像素值的边距设置画线呢?

示例代码如下:

        PdfContentByte contentByte = writer.DirectContent;
        contentByte.SetLineWidth(1);
        float x1, y1, x2, y2;
        x1 = myDocument.PageSize.Width - 84;
        x2 = myDocument.PageSize.Width - 36;
        y1 = myDocument.PageSize.Height - 56;
        y2 = myDocument.PageSize.Height - 56;
        contentByte.MoveTo(x1, y1);
        contentByte.LineTo(x2, y2);
        contentByte.Stroke();

其实我想画的线宽是48,右边距是36px,上边距是36px。

有什么计算方法吗?

【问题讨论】:

    标签: c# .net pdf itextsharp


    【解决方案1】:

    试试这个方法:

    string pdfpath = Server.MapPath("PDFs");
      Document doc = new Document();
      try
      {
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Graphics.pdf", FileMode.Create));
        doc.Open();
        PdfContentByte cb = writer.DirectContent;
        ...
    

    现在我们有了一个工作的 PdfContentByte 对象,我们可以使用它开始绘图:

    cb.MoveTo(doc.PageSize.Width / 2, doc.PageSize.Height / 2);
    cb.LineTo(doc.PageSize.Width / 2, doc.PageSize.Height);
    cb.Stroke();
    cb.MoveTo(0, doc.PageSize.Height/2);
    cb.LineTo(doc.PageSize.Width, doc.PageSize.Height / 2);
    cb.Stroke();
    

    取自here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多