我要创建一个新的pdf文件,添加一个水印,然后使每个页面成为一个图像页面。
itext可以吗?
答案 0 :(得分:0)
我不知道如何将其转换为图像,但是对于水印,正如Usama Kiyani在评论中所说,您应该考虑使用可以通过块软件包管理器安装的itextsharp。我已经使用它在现有的pdf文件中添加水印。
这是我使用的代码,它在现有pdf文件(sourceFile)的每页中央添加一个对角红色水印(文本为自变量watermarkText),然后将此修改后的版本保存在给定位置(outputFile):
public static void AddWatermarkTextC(string sourceFile, string outputFile, string watermarkText)
{
BaseFont tWatermarkFont = null;
float tWatermarkFontSize = 48F;
iTextSharp.text.BaseColor tWatermarkFontColor = null;
float tWatermarkFontOpacity = 0.3F;
float tWatermarkRotation = 45.0F;
tWatermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
tWatermarkFontColor = iTextSharp.text.BaseColor.RED;
AddWatermarkTextC(sourceFile, outputFile, watermarkText, tWatermarkFont, tWatermarkFontSize, tWatermarkFontColor, tWatermarkFontOpacity, tWatermarkRotation);
}
public static void AddWatermarkTextC(string sourceFile, string outputFile, string watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float watermarkFontSize, iTextSharp.text.BaseColor watermarkFontColor, float watermarkFontOpacity, float watermarkRotation)
{
iTextSharp.text.pdf.PdfReader reader = null;
iTextSharp.text.pdf.PdfStamper stamper = null;
iTextSharp.text.pdf.PdfGState gstate = null;
iTextSharp.text.pdf.PdfContentByte underContent = null;
iTextSharp.text.Rectangle rect = null;
float currentY = 0.0F;
float offset = 0.0F;
int pageCount = 0;
try
{
reader = new iTextSharp.text.pdf.PdfReader(sourceFile);
rect = reader.GetPageSizeWithRotation(1);
FileStream stream = new System.IO.FileStream(outputFile, System.IO.FileMode.Create);
stamper = new iTextSharp.text.pdf.PdfStamper(reader, stream);
if (watermarkFont == null)
{
watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
}
if (watermarkFontColor == null)
{
watermarkFontColor = iTextSharp.text.BaseColor.RED;
}
gstate = new iTextSharp.text.pdf.PdfGState();
gstate.FillOpacity = watermarkFontOpacity;
gstate.StrokeOpacity = watermarkFontOpacity;
pageCount = reader.NumberOfPages;
for (int i = 1; i <= pageCount; i++)
{
underContent = stamper.GetOverContent(i);
underContent.SaveState();
underContent.SetGState(gstate);
underContent.SetColorFill(watermarkFontColor);
underContent.BeginText();
underContent.SetFontAndSize(watermarkFont, watermarkFontSize);
underContent.SetTextMatrix(30, 30);
currentY = (rect.Height / 2);
underContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText, rect.Width / 2, currentY - offset, watermarkRotation);
underContent.EndText();
underContent.RestoreState();
}
stamper.Close();
reader.Close();
stream.Close();
}
catch (Exception ex)
{
throw ex;
}
}
我想根据您的需要对其进行更改并不是很困难,但是如果有任何需要我解释的内容,请提出要求。