使用iText Sharp将图像插入现有的pdf文档中

我需要将图像插入到特定位置的现有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)

2 years ago

我调整了你的方法来接受变量输出路径和位置,并使用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不起作用,他会保留我们需要帮助他的信息 .

Multipage-stamp-Image-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);

Multipage-stamp-Image-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);