【问题标题】:How to absolute position the image in existing pdf using itextsharp如何使用 itextsharp 在现有 pdf 中绝对定位图像
【发布时间】:2011-10-28 02:39:45
【问题描述】:

这是我目前的代码:

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO

Module Module1

    Sub Main()
        AddjImage("C:\test.png", "c:\pdfTemplate.pdf", "C:\output.pdf")
    End Sub
    Private Function AddjImage(ByVal strImageFileName As String, ByVal pdfTemplateFile As String, ByVal outputPdf As String) As Boolean
        Try
            Dim iPdfReader As PdfReader = New PdfReader(pdfTemplateFile)
            Dim iPdfStamper As PdfStamper = New PdfStamper(iPdfReader, New FileStream(outputPdf, FileMode.Create))
            Dim imgjImage As iTextSharp.text.Image
            Dim bytContent As PdfContentByte
            'Insert Image
            imgjImage = iTextSharp.text.Image.GetInstance(strImageFileName)
            imgjImage.Alignment = iTextSharp.text.Image.ALIGN_TOP
            imgjImage.ScalePercent(78)
            imgjImage.SetAbsolutePosition(445, 0)
            bytContent = iPdfStamper.GetOverContent(1)
            bytContent.AddImage(imgjImage)
            iPdfStamper.FormFlattening = True
            iPdfStamper.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function    
End Module

pdf 为横向布局。页面大小为 A4。我正在尝试在 pdf 页面的右侧插入图像。我想在 x=445 和 y=0 位置对齐图像。

我有两个尺寸的图片。他们是: 图片 1,宽度 = 500px;高度=910px; 宽度为 500 像素的图像 2;高度=400px;

问题是,两个图像都对齐到底部而不是顶部。因为图片1的顶部被切断了。

【问题讨论】:

    标签: vb.net itextsharp


    【解决方案1】:

    我尝试了您的代码(经过修改)以适应我在 wpf 应用程序中的按钮单击事件。必须更改下面的行以使图像上升。我感觉你用的0是从底部开始的。

    imgjImage.SetAbsolutePosition(445, 0)
    

    改成

    imgjImage.SetAbsolutePosition(445, 200)
    

    200 不是绝对的,它必须根据您的图像实际尺寸重新调整。

    【讨论】:

      猜你喜欢
      • 2012-12-21
      • 2015-06-23
      • 2010-10-09
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多