使用itextsharp在现有pdf中插入文本 [英] Insert text in existing pdf with itextsharp

查看:474
本文介绍了使用itextsharp在现有pdf中插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在现有的pdf中添加文字,如下面的网址所示。
但是当我添加文本时,文本会保留在pdf中的图像下方。我该如何解决这个问题呢?



解决方案

两条评论:


  1. 你添加文字首先,然后添加图像。因此图像覆盖了文本。这是基本逻辑。如果您切换顺序并首先添加图像,然后是文本,文本将覆盖图像。这是纯粹的常识。

  2. 您通过导入 PdfImportedPage PdfWriter 。这证明你没有阅读文档。你应该使用 PdfStamper

你的代码太复杂了。切换到 PdfStamper 并使用 ColumnText 对象添加文本。不要使用 BeginText() / EndText()。另外:你为什么用 EndLayer() ???你知道那个方法的用途是什么吗?


I can add a text to an existing pdf as it is explained in the url below. But the texts stay below the images in the pdf when I add them. How can I fix this problem?

ITextSharp insert text to an existing pdf

Edit :

  public void createFromPDF(string mapPath)
    {
        string oldFile = mapPath.Replace("Home", "") + "Content\\Uploads\\fiyat-listesi.pdf";// "oldFile.pdf";
        string newFile = mapPath.Replace("Home", "") + "Content\\Uploads\\new.pdf";//"newFile.pdf";

        // open the reader
        PdfReader reader = new PdfReader(oldFile);
        Rectangle size = reader.GetPageSizeWithRotation(1);
        Document document = new Document(size);

        // open the writer
        FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
        PdfWriter writer = PdfWriter.GetInstance(document, fs);
        document.Open();

        // the pdf content
        PdfContentByte cb = writer.DirectContent;

        // select the font properties
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.SetColorFill(Color.RED);
        cb.SetFontAndSize(bf, 8);

        // write the text in the pdf content
        cb.BeginText();
        string text = "Some random blablablabla...";
        // put the alignment and coordinates here
        cb.ShowTextAligned(1, text, 520, 640, 0);
        cb.EndText();
        cb.BeginText();
        text = "Other random blabla...";
        // put the alignment and coordinates here
        cb.ShowTextAligned(2, text, 100, 200, 0);
        cb.EndText();

        // create the new page and add it to the pdf
        PdfImportedPage page = writer.GetImportedPage(reader, 1);
        cb.AddTemplate(page, 0, 0);

        document.NewPage();

        Paragraph p = new Paragraph("aaaaaaaaaaaaaaaaaa", new Font(bf));
        document.Add(p);


        PdfImportedPage page2 = writer.GetImportedPage(reader, 2);
        cb.AddTemplate(page2, 0, 0);

        document.NewPage();


        Paragraph pwe = new Paragraph("aaaaaaaaaaaaaaaaaa", new Font(bf));
        document.Add(p);

        cb.EndLayer();

        PdfImportedPage page3 = writer.GetImportedPage(reader, 3);
        cb.AddTemplate(page3, 0, 0);



        // close the streams and voilá the file should be changed :)
        document.Close();
        fs.Close();
        writer.Close();
        reader.Close();
    }

解决方案

Two remarks:

  1. You add the text first, then you add the image. Hence the image covers the text. That's elementary logic. If you switch the order and add the image first, then the text, the text will cover the image. That's pure common sense.
  2. You manipulate an existing PDF by importing a PdfImportedPage and PdfWriter. That proves thaf you didn't read the documentation. You should use PdfStamper instead!

Your code is too complex. Switch to PdfStamper and add text using the ColumnText object. Don't use BeginText() / EndText(). Also: why are you using EndLayer()??? Do you have any idea what that method is meant for?

这篇关于使用itextsharp在现有pdf中插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆