我正在使用此方法将文本添加到已创建的pdf文档。 ITextSharp insert text to an existing pdf 基本上它使用PdfContentByte,然后将内容模板添加到页面。ITextSharp添加文本。一些文本没有显示
我发现在文件的某些区域中,文本不显示。 看来我正在添加的文本显示在已经在页面上的内容后面?我把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();
感谢您提供的任何帮助......我真的很感激它! -Greg
感谢您的帮助!我不太明白我使用的代码,并没有意识到我在新内容的顶部覆盖了页面内容。我只是移动了代码,将页面内容覆盖在我的所有绘图之上,现在它工作正常。将来我还会检查一下PdfStamper。 – smithygreg 2013-05-02 14:56:33
@smithygreg你可以请发表新的代码或更改代码 – 2015-01-02 08:49:35