【问题标题】:ITextSharp adding text. Some text not showing upITextSharp 添加文本。有些文字没有显示
【发布时间】:2013-04-26 02:53:17
【问题描述】:

我正在使用此方法将文本添加到已创建的 pdf 文档中。 ITextSharp insert text to an existing pdf 基本上它使用 PdfContentByte,然后将内容模板添加到页面。

我发现文件的某些区域没有显示文本。 我添加的文本似乎显示在页面上已有的内容后面?我将 pdf 文档扁平化为图像,但扁平化文件仍然存在同样的问题。

是否有人在使用 Itextsharp 添加隐藏文本时遇到任何问题?

我也尝试使用此链接中建议的 DirectContentUnder 无济于事.. iTextSharp hides text when write

这是我正在使用的代码...有了这个,我试图基本上将方格纸覆盖在 PDF 之上。在此示例中,每个页面的左上角都有一个未填充的框。这个地方的原始pdf中有一张图片。在第 4 和第 5 页,有些框没有填充,但它们似乎不是图像。

PdfReader reader = new PdfReader(oldFile);
iTextSharp.text.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;


for (int i = 0; i < reader.NumberOfPages; i++)
{
    document.NewPage();
    // select the font properties
    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

    cb.SetFontAndSize(bf, 4);
    cb.SetColorStroke(BaseColor.GREEN);
    cb.SetLineWidth(1f);
    for (int j = 10; j < 600; j += 10)
    {
        WriteToDoc(ref cb, j.ToString(), j, 10);//Write the line number
        WriteToDoc(ref cb, j.ToString(), j, 780);//Write the line number
        if (j % 20 == 0)
        {
            cb.MoveTo(j, 20);
            cb.LineTo(j, 760);
            cb.Stroke();
        }
    }
    for (int j = 10; j < 800; j += 10)
    {

       WriteToDoc(ref cb, j.ToString(), 5, j);//Write the line number
       WriteToDoc(ref cb, j.ToString(), 590, j);//Write the line number
       if (j % 20 == 0)
       {
             cb.MoveTo(15, j);
             cb.LineTo(575, j);
             cb.Stroke();
       }
    }




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

 }

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

感谢您提供的任何帮助...非常感谢! -格雷格

【问题讨论】:

    标签: pdf itextsharp


    【解决方案1】:

    首先:如果您试图将方格纸基本覆盖在 PDF 之上, 为什么要先绘制方格纸并将原始页面盖在上面?您实际上是在方格纸上,而不是在上。

    根据页面的内容,这种方式的方格纸可能很容易被覆盖。例如。如果页面内容中有一个填充的矩形,则结果每个页面的左上角都有一个未填充的框。

    因此,只需先添加旧页面内容,然后添加覆盖更改。

    话虽如此,对于将更改应用于现有 PDF 的任务,使用 PdfWriterGetImportedPage 并不是最佳选择。这实际上是 PdfStamper 类的一项任务,它用于在现有 PDF 上标记其他内容。

    例如看看sample StampText,关键代码是:

    PdfReader reader = new PdfReader(resource); 
    using (var ms = new MemoryStream())
    {
      using (PdfStamper stamper = new PdfStamper(reader, ms))
      {
        PdfContentByte canvas = stamper.GetOverContent(1);
        ColumnText.ShowTextAligned( canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0 );
      }
      return ms.ToArray();
    }
    

    【讨论】:

    • 感谢您的帮助!我不太了解我正在使用的代码,也没有意识到我将页面内容覆盖在我的新内容之上。我只是移动了代码以将页面内容覆盖在我所有的绘图之上,现在它工作正常。将来我也会检查 PdfStamper。
    • @smithygreg 您能否发布新代码或代码更改
    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多