【问题标题】:iTextSharp Scaling image to be full-pageiTextSharp 将图像缩放为整页
【发布时间】:2011-02-08 10:58:53
【问题描述】:

我正在尝试将图像缩放为 PDF 文档的整页。我正在使用 iTextSharp 生成文档。该图像具有正确的页面纵横比,但我更希望图像扭曲而不是填充所有可用区域。

我目前有:

Dim Document As New Document(PageSize, 0, 0, 0, 0)
...
Dim ContentImage = '''Method call to get image'
Dim Content = iTextSharp.text.Image.GetInstance(ContentImage, New BackgroundColor)
Content.SetAbsolutePosition(0, 0)
Content.ScaleToFit(Document.PageSize.Width, Document.PageSize.Height)
Document.Add(Content)

很遗憾,这不考虑打印机边距...

我需要图像适合可打印区域(尽可能在 pdf 中定义)

提前致谢

【问题讨论】:

    标签: pdf .net-4.0 itext


    【解决方案1】:

    如果您决定凭经验进行操作,则使用您的代码打印一个页面,该页面按原样缩放到页面边框,以便图像在前半英寸的边距中涂黑,如果它可以到达边缘.以英寸为单位测量从每个边缘到黑色的距离,然后将每个边缘除以 72.0。

    让我们为它们命名:lm、rm、tm、bm(左右上下边距。

    Dim pageWidth = document.PageSize.Width - (lm + rm);
    Dim pageHeight = document.PageSize.Height - (bm + tm);
    Content.SetAbsolutePosition(lm, bm);
    Content.ScaleToFit(pageWidth, pageHeight);
    Document.Add(Content)
    

    【讨论】:

      【解决方案2】:

      您可以使用以下代码 sn-p 缩放图像以适合 PDF 页面。

      VB

      Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(resourceStream), System.Drawing.Imaging.ImageFormat.Png)
      img.SetAbsolutePosition(0, 0) 
      'set the position to bottom left corner of pdf
      img.ScaleAbsolute(iTextSharp.text.PageSize.A7.Width, iTextSharp.text.PageSize.A7.Height)
      'set the height and width of image to PDF page size
      

      C#

      iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(resourceStream, System.Drawing.Imaging.ImageFormat.Png);
      img.SetAbsolutePosition(0, 0); // set the position to bottom left corner of pdf
      img.ScaleAbsolute(iTextSharp.text.PageSize.A7.Width,iTextSharp.text.PageSize.A7.Height); // set the height and width of image to PDF page size
      

      如果您想要完整的代码(c#),您也可以参考以下链接。完整代码将图像添加到现有 PDF 的所有页面。

      https://stackoverflow.com/a/45486484/6597375

      【讨论】:

        【解决方案3】:

        可打印区域取决于打印机,PDF 文件对此一无所知。 PDF 页面可以包含从页边到页边的内容。您可以使用“适合打印机边距”选项打印 PDF 文件,以便将整个 PDF 页面按比例打印到打印机的可打印区域。

        【讨论】:

        • 了解 - 但我正在使用(最初)自动打印:Writer.AddJavaScript("this.print(false);", False),并且希望避免手动设置缩放选项。这可以自动化吗?在这种情况下,它用于在单个打印机上打印内部文档,因此我也可以接受必须知道适当的边距并在生成过程中在 PDF 上设置它们。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-05
        • 1970-01-01
        • 1970-01-01
        • 2011-05-18
        • 1970-01-01
        • 1970-01-01
        • 2023-01-26
        相关资源
        最近更新 更多