c#.NET CORE使用ITextSharp为现有PDF添加透明图像 [英] c# .NET CORE adding image with transparency to existing PDF using ITextSharp

查看:1982
本文介绍了c#.NET CORE使用ITextSharp为现有PDF添加透明图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在现有pdf(非水印)的每个页面上添加公司徽标。

My goal is to add company logo to every page of an existing pdf(not watermark).

由于pdf文件和徽标细节,我只能放置pdf内容(不在下面)上面的徽标,徽标必须支持透明度。

Due to pdf file and logo specifics, I can only place the logo on top of the pdf content(not underneath) and the logo has to support transparency.

另一个限制是我必须使用.NET Core。

One more limitation is I have to use .NET Core.

发布这个答案,因为我找不到明确的解决方案。
建议/更正/改进是受欢迎的。

Posting this with an answer, because I could not find a clear solution. Suggestions/corrections/improvements are welcome.

希望有人觉得这很有用。

Hope someone finds this useful.

推荐答案

支持.NET Core的最新iTextSharp库是 iText7 ,但我不能合法使用它;既不是我的代码开源,也不是购买许可证是我的选择。因此我使用旧的第三方库:

The newest iTextSharp library to support .NET Core is iText7 however I cannot use it legitemately; neither making my code open source, nor purchasing the licence is an option for me. Therefore I use old, third party library:

Install-Package iTextSharp.LGPLv2.Core

我发布的最新版本是1.3.2

Latest version, the one I'm using, at the time of this post is 1.3.2

需要使用以下

using System;
using System.Drawing.Imaging;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

要获得pdf中的图像透明度,必须以正确的格式打开图像

To acheve image transparency in pdf, image has to be opened in a correct format

var preImage = System.Drawing.Image.FromFile(imagePath);
var image = Image.GetInstance(preImage, ImageFormat.Png);

添加图片时,选择要内嵌的图片也很重要

When adding the image, it is also important to not select the image to be inline

canvas.AddImage(image);//do not put .AddImage(image, true);

以下是所有代码

var imagePath = "logo.png";
var pdfPath = "edit_this.pdf";

//load pdf file
var pdfBytes = File.ReadAllBytes(pdfPath);
var oldFile = new PdfReader(pdfBytes);

//load image
var preImage = System.Drawing.Image.FromFile(imagePath);
var image = Image.GetInstance(preImage, ImageFormat.Png);
preImage.Dispose();

//optional: if image is wider than the page, scale down the image to fit the page
var sizeWithRotation = oldFile.GetPageSizeWithRotation(1);
if (image.Width > sizeWithRotation.Width)
    image.ScalePercent(sizeWithRotation.Width / image.Width * 100);

//set image position in top left corner
//in pdf files, cooridinates start in the left bottom corner
image.SetAbsolutePosition(0, sizeWithRotation.Height - image.ScaledHeight);

//in production, I use MemoryStream
//I put FileStream here to test the code in console application
using (var newFileStream = new FileStream("with_logo.pdf", FileMode.Create))
{
    //setup PdfStamper
    var stamper = new PdfStamper(oldFile, newFileStream);

    //iterate through the pages in the original file
    for (var i = 1; i <= oldFile.NumberOfPages; i++)
    {
        //get canvas for current page
        var canvas = stamper.GetOverContent(i);
        //add image with pre-set position and size
        canvas.AddImage(image);
    }

    stamper.Close();
}

此代码适用于本地文件。
在我的(真实世界)情况下,我收到pdf文件作为Base64字符串,从本地存储添加徽标,将其转换回Base64字符串并将其输出到网页上。

This code works with local files. In my (real world) case, I receive pdf files as Base64 string, add a logo from local storage, convert it back to Base64 string and output it on a web-page.

我强行打开图像(硬编码)因为我控制了徽标的扩展名。如有必要,您可以动态设置图像格式。

I open the image as PNG forcefully(hardcoded) because I control what extension does the logo have. If necessary you can dynamicaly set the image format.

这篇关于c#.NET CORE使用ITextSharp为现有PDF添加透明图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆