【问题标题】:Setting image position using iTextSharp使用 iTextSharp 设置图像位置
【发布时间】:2013-04-24 01:28:18
【问题描述】:

我对纸张尺寸的页面方向有疑问。
我有一个包含 portraitlandscape 页面的 pdf 文件。

这段代码完美运行。

string FileLocation = "c:\\Temp\\SomeFile.pdf";
string WatermarkLocation = "c:\\Temp\\watermark.gif";
Document document = new Document();
PdfReader pdfReader = new PdfReader(FileLocation);
PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(FileLocation.Replace(".pdf","[temp][file].pdf"), FileMode.Create));

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(WatermarkLocation);
img.SetAbsolutePosition(250,300); // set the position in the document where you want the watermark to appear (0,0 = bottom left corner of the page)

PdfContentByte waterMark;
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
    waterMark = stamp.GetUnderContent(page);
    waterMark.AddImage(img);
}
stamp.FormFlattening = true;
stamp.Close();

// now delete the original file and rename the temp file to the original file
File.Delete(FileLocation);
File.Move(FileLocation.Replace(".pdf", "[temp][file].pdf"), FileLocation);

因为我使用绝对值来设置图像位置。

img.SetAbsolutePosition(250,300);

如果页面是横向或纵向,如何设置图像位置?
注意:一份具有横向和纵向页面方向的 pdf。

是否有机会我可以使用 if 语句?

if (//paper is landscape)
{
    //code here
}
else 
{
    //code here
}

【问题讨论】:

    标签: c# itext


    【解决方案1】:

    你想达到什么目标?

    通常,iText 会考虑页面旋转的值。这意味着当页面旋转时,坐标也会旋转。

    如果你想推翻这一点,你可以添加这一行:

    stamper.RotateContents = false;
    

    这在Chapter 6 of my book 中有解释。你可以试试this example看看有什么区别:

    1. 无旋转,文字添加正常:hello1.pdf
    2. 旋转,正常添加的文本(= 旋转):hello2.pdf
    3. 旋转,忽略旋转添加的文本:hello3.pdf

    当然,这假定为页面定义了旋转。有时,通过定义不同的页面大小而不是定义旋转来模仿横向。

    在这种情况下,您还应该阅读Chapter 6,因为它解释了如何获取文档的 MediaBox。请参阅示例PageInformation,其中介绍了GetPageSize()GetRotation()GetPageSizeWithRotation() 等方法。

    这一切都有记录,但如果它不能回答您的问题,请澄清。如示例所示,添加新内容时默认考虑轮换,所以我可能误解了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多