简体   繁体   English

C#.NET核心使用ITextSharp向现有PDF添加具有透明度的图像

[英]c# .NET CORE adding image with transparency to existing PDF using ITextSharp

My goal is to add company logo to every page of an existing pdf(not watermark). 我的目标是在现有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. 由于pdf文件和徽标的特殊性,我只能将徽标放在pdf内容的顶部(而不是其下方),并且徽标必须支持透明性。

One more limitation is I have to use .NET Core. 另一个限制是我必须使用.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. 希望有人觉得这有用。

The newest iTextSharp library to support .NET Core is iText7 however I cannot use it legitemately; 支持.NET Core的最新iTextSharp库是iText7,但是我不能合法地使用它。 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

Latest version, the one I'm using, at the time of this post is 1.3.2 我正在使用的最新版本是1.3.2。

Following usings are required 需要以下用途

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

To acheve image transparency in pdf, image has to be opened in a correct format 为了使图像具有pdf透明度,必须以正确的格式打开图像

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);

Here is all the code 这是所有代码

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();
}

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. 在我的现实世界中,我收到作为Base64字符串的pdf文件,从本地存储中添加徽标,将其转换回Base64字符串,并将其输出到网页上。

I open the image as PNG forcefully(hardcoded) because I control what extension does the logo have. 我强制(硬编码)以PNG格式打开图片,因为我可以控制徽标的扩展名。 If necessary you can dynamicaly set the image format. 如有必要,您可以动态设置图像格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM