我想将图像位置更改为 pdf 的右上角作为徽标,此代码使我的图像位于左上角,并在其上写了一些文字:
string oldFile = @"D:\Source pdf\Source.pdf";
string newFile = @"D:\Result pdf\Result.pdf";
string imagepath = @"D:\Source pdf\Myiamge.jpg";
Console.WriteLine("iText Demo");
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
iTextSharp.text.Image instanceImg = iTextSharp.text.Image.GetInstance(imagepath);
document.Add(instanceImg);
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.DARK_GRAY);
cb.SetFontAndSize(bf, 8);
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
document.Close();
fs.Close();
writer.Close();
reader.Close();