如何使用iTextSharp更改pdf中添加图像的位置

时间:2013-01-29 13:53:27

标签: c# itextsharp

我想将图像位置更改为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(); 

0 个答案:

没有答案