pdf - ITextSharp 添加文本。部分文字不显示

标签 pdf itextsharp

我正在使用此方法将文本添加到已创建的 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 之上基本上覆盖方格纸,为什么要先绘制方格纸并将原始页面盖在其上?您本质上是在方格纸下放置,而不是覆盖它。

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

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

话虽如此,对于将更改应用于现有 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();
}

关于pdf - ITextSharp 添加文本。部分文字不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16329488/

相关文章:

javascript - 使用 iTextSharp 将 javascript 添加到 pdf 文件

c# - 错误,因为输入字符串的格式不正确

vb.net - itextsharp:如何添加新页面并写入其中?

wpf - PdfWriter.GetInstance 抛出 System.NullReferenceException

Python PyPDF2 合并旋转页面

javascript - 使用 pdf.js 渲染 pdf 时如何重新设置单词样式?

python - 无法在python中使用FPDF打印特定字符

qt - 在 Qt 中,我可以 QWebFrame::print() 提供额外的渲染层吗?

c# - 使用 itextsharp 检查 pdf 是否受密码保护

c# - 使用 iTextSharp 从 PDF 中去除水印