背景: 該PDF 文件是利用ITexhsharp組件, 在已有PDF基礎上添加水印生成。
問題:
1、PDF顯示有內容但用AI打開無內容;
2、PDF左邊顯示圖層名:WatermarkLayer, 而用AI打開顯示圖層名:Layer 1 ;
PDF如圖1:
圖1
AI打開如圖2:
圖2
源碼如下:
string watermarkedFile = "d://Watermarked.pdf";
string originalFile = @"d:/fdsf.pdf";
PdfReader reader1 = new PdfReader(originalFile);
using (FileStream fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (PdfStamper stamper = new PdfStamper(reader1, fs))
{
int pageCount = reader1.NumberOfPages;
PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
for (int i = 1; i <= pageCount; i++)
{
iTextSharp.text.Rectangle rect = reader1.GetPageSize(i);
PdfContentByte cb = stamper.GetUnderContent(i);
cb.BeginLayer(layer);
cb.SetFontAndSize(BaseFont.CreateFont(
BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
PdfGState gState = new PdfGState();
gState.FillOpacity = 0.25f;
cb.SetGState(gState);
cb.SetColorFill(BaseColor.BLACK);
cb.BeginText();
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "stsgs", rect.Width/2,rect.Height / 2, 45f);
cb.EndText();
cb.EndLayer();
}
}
为什么要用AI打开,用PS打开多好?!
因為PDF要印刷出來,而PDF可能包括多個圖層,該印刷機要識別PDF的圖層名來印刷,用AI打開是想看看PDF有沒有實現了內容分層。