Pdf 添加文本。某些文本未显示
我正在使用此方法将文本添加到已创建的pdf文档中。 基本上,它使用PdfContentByte,然后将内容模板添加到页面中 我发现在文件的某些区域,文本没有显示出来。 我添加的文本似乎显示在页面上已有的内容后面?我将pdf文档展平为图像,但展平后的文件仍然存在同样的问题 是否有人在使用Itextsharp添加隐藏文本时遇到任何问题 我也尝试过使用DirectContentUnder,正如在这个链接中建议的那样,但没有任何效果。。 这是我正在使用的代码…我试图用它基本上覆盖在PDF上的图表纸。在本例中,每个页面的左上角都有一个未填充的框。在这个地方有一个原始pdf中的图像。在第4页和第5页,有一些框没有填充,但它们似乎不是图像Pdf 添加文本。某些文本未显示,pdf,itextsharp,Pdf,Itextsharp,我正在使用此方法将文本添加到已创建的pdf文档中。 基本上,它使用PdfContentByte,然后将内容模板添加到页面中 我发现在文件的某些区域,文本没有显示出来。 我添加的文本似乎显示在页面上已有的内容后面?我将pdf文档展平为图像,但展平后的文件仍然存在同样的问题 是否有人在使用Itextsharp添加隐藏文本时遇到任何问题 我也尝试过使用DirectContentUnder,正如在这个链接中建议的那样,但没有任何效果。。 这是我正在使用的代码…我试图用它基本上覆盖在PDF上的图表纸。在
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();
PdfReader reader=新的PdfReader(旧文件);
iTextSharp.text.Rectangle size=reader.GetPageSizeWithRotation(1);
文件=新文件(大小);
//打开书写器
FileStream fs=newfilestream(newFile,FileMode.Create,FileAccess.Write);
PdfWriter writer=PdfWriter.GetInstance(文档,fs);
document.Open();
//pdf内容
PdfContentByte cb=writer.DirectContent;
对于(int i=0;i
谢谢你能提供的任何帮助…我真的很感激!
-格雷格首先:如果你想基本上在PDF上覆盖一张图表纸,为什么你要先画一张图表纸,然后在上面盖上原始页面?实际上,您是在图形纸的下面,而不是在上面
根据页面内容的不同,您的图表纸可能很容易被覆盖。例如,如果页面内容中有一个填充矩形,则结果是每个页面的左上角都有一个未填充的框
因此,只需先添加旧页面内容,然后添加覆盖更改
也就是说,对于将更改应用到现有PDF的任务,使用PdfWriter
和GetImportedPage
并不是最理想的。这实际上是PdfStamper
类的任务,该类用于在现有PDF上标记附加内容
例如,看一下,关键代码是:
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您能发布新代码或代码中的更改吗