使用iText Sharp

时间:2015-11-30 09:43:18

标签: image itextsharp

我需要将图像插入特定位置的现有pdf。我在this问题上尝试了答案。但无论我做什么不同的方式,图像都被插入(0,0)位置(左下角)。我尝试了另一种方法,而不是使用流我在iTextSharp中使用了Document类,如here所示。现在我可以将图像放在所需的位置,但这种方法只是用这个图像创建一个新文档。我搜索的大多数文章都使用PdfReader和PdfStamper,所以我认为这是推荐的方法。任何帮助表示赞赏。在我尝试的两种方法的代码下面发布。

PdfStamper方法

   private void AddImage(string filePath)
    {
        string imageURL = @"ImagePath\Image.jpg";

        using (Stream inputPdfStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        using (Stream inputImageStream = new FileStream(imageURL, FileMode.Open, FileAccess.Read))
        using (Stream outputPdfStream = new FileStream(@"ResultingPdfPath\Abcd.pdf", FileMode.Create, FileAccess.ReadWrite))
        {
            Image image = Image.GetInstance(inputImageStream);
            image.ScaleToFit(100, 100);

            var reader = new PdfReader(inputPdfStream);
            var stamper = new PdfStamper(reader, outputPdfStream);

            PdfContentByte content = stamper.GetUnderContent(1);
            image.SetAbsolutePosition(100f, 150f);
            content.AddImage(image);

            stamper.Close();

            reader.Close();
        }
    }

文档类方法

    private void TestMessage(string filePath)
    {
        string imageURL = @"ImagePath\Image.jpg";

        Document doc = new Document(PageSize.A4);

        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Open));

        doc.Open();

        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
        jpg.ScaleToFit(140f, 120f);
        jpg.SetAbsolutePosition(100, 100);
        jpg.SpacingBefore = 10f;
        jpg.SpacingAfter = 1f;
        jpg.Alignment = Element.ALIGN_LEFT;
        doc.Add(jpg);
        doc.Close();
    }

如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

我调整了你的方法来接受变量输出路径和位置,并用iTextSharp 5.5.7测试它,如下所示:

[TestFixture]
class TestInsertImage
{
    /// iText stamp image on top not always working
    /// http://stackoverflow.com/questions/33898280/itext-stamp-image-on-top-not-always-working
    /// 
    [Test]
    public void AddStampToTestPdf()
    {
        Directory.CreateDirectory(@"C:\Temp\test-results\content\");

        AddImage(@"d:\Issues\stackoverflow\iText stamp image on top not always working\Multipage.pdf", @"C:\Temp\test-results\content\Multipage-stamp-Image-100-150.pdf", 100f, 150f);
        AddImage(@"d:\Issues\stackoverflow\iText stamp image on top not always working\Multipage.pdf", @"C:\Temp\test-results\content\Multipage-stamp-Image-150-100.pdf", 150f, 100f);
    }

    private void AddImage(string filePath, string outPath, float x, float y)
    {
        string imageURL = @"c:\Repo\GitHub\testarea\itext5\src\test\resources\mkl\testarea\itext5\layer\Willi-1.jpg";

        using (Stream inputPdfStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        using (Stream inputImageStream = new FileStream(imageURL, FileMode.Open, FileAccess.Read))
        using (Stream outputPdfStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite))
        {
            Image image = Image.GetInstance(inputImageStream);
            image.ScaleToFit(100, 100);

            var reader = new PdfReader(inputPdfStream);
            var stamper = new PdfStamper(reader, outputPdfStream);

            PdfContentByte content = stamper.GetUnderContent(1);
            image.SetAbsolutePosition(x, y);
            content.AddImage(image);

            stamper.Close();

            reader.Close();
        }
    }
}

结果如下。

如您所见,定位信息明显受到尊重,图像肯定是 始终位于左下角

如果这确实对OP不起作用,他会向我们提供帮助他的信息。

缩-印章图像 - 100-150.pdf

At 100, 150

使用

创建
AddImage(@"d:\Issues\stackoverflow\iText stamp image on top not always working\Multipage.pdf", @"C:\Temp\test-results\content\Multipage-stamp-Image-100-150.pdf", 100f, 150f);

缩-印章图像 - 150-100.pdf

At 150, 100 使用:

创建
AddImage(@"d:\Issues\stackoverflow\iText stamp image on top not always working\Multipage.pdf", @"C:\Temp\test-results\content\Multipage-stamp-Image-150-100.pdf", 150f, 100f);