简体   繁体   English

使用iTextsharp错误将行添加到pdf无法看到行

[英]Adding a Line to a pdf using iTextsharp Error cant see the Line

I have a PDF File and I am trying to put a line in it using iTextSharp like this. 我有一个PDF文件,我正在尝试使用iTextSharp这样在其中添加一行。

string Oldfile = @"C:\ThisTest.pdf";
    string NewFile = @"C:\NewOne.pdf";
    PdfReader reader = new PdfReader(Oldfile);
    iTextSharp.text.Rectangle Size = reader.GetPageSizeWithRotation(1);
    Document document = new Document(Size);
    FileStream fs = new FileStream(NewFile, FileMode.Create, FileAccess.Write);
    PdfWriter weiter = PdfWriter.GetInstance(document, fs);
    document.Open();
    PdfContentByte cb = weiter.DirectContent;
    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
    cb.SetColorFill(BaseColor.DARK_GRAY);
    cb.SetFontAndSize(bf, 8);
    cb.BeginText();
    string text = "Haseeb Ahmad";
    cb.ShowTextAligned(1, text, 520, 640, 0);
    cb.EndText();
    PdfImportedPage page = weiter.GetImportedPage(reader, 1);
    cb.AddTemplate(page, 0, 0);
    document.Close();
    fs.Close();
    weiter.Close();
    reader.Close();

I am trying to add a line to the end of this Document but after it does that code it creates the file and when I open it i cant see that text. 我正在尝试在此文档的末尾添加一行,但是在执行该代码后,它将创建该文件,而当我打开它时,我看不到该文本。 When i do a search for that word it says its's there. 当我搜索该词时,它说它在那里。 Like this 像这样 搜索显示该行在这里,但我看不到它

That mean the code worked all the problem is the font can any one tell me how to make it come to the end of the page and show up instead of being hidden ? 这意味着代码可以解决所有问题,即字体是谁能告诉我如何使它到达页面的末尾并显示而不是隐藏?

Your code first draws the text 您的代码首先绘制文本

cb.SetColorFill(BaseColor.DARK_GRAY);
cb.SetFontAndSize(bf, 8);
cb.BeginText();
string text = "Haseeb Ahmad";
cb.ShowTextAligned(1, text, 520, 640, 0);
cb.EndText();

and thereafter copies the original page into this 然后将原始页面复制到此

PdfImportedPage page = weiter.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);

Thus, your original page contents (which may include a white background fill) cover your new text. 因此,您的原始页面内容(可能包括白色背景填充)覆盖了您的新文本。

Please change the order, copy the page first and then write the text. 请更改顺序,首先复制页面,然后输入文字。


If your task is indeed putting some text onto existing pages of an existing PDF , you should use other iText classes entirely: PdfStamper is made for just this kind of task while the PdfWriter is more made for creating new PDFs from scratch. 如果您的任务确实是在现有PDF的现有页面上放置一些文本 ,则应完全使用其他iText类: PdfStamper用于此类任务,而PdfWriter则更多用于从头创建新PDF。

Your approach is quite lossy (it drops document level information and interactive parts like form fields) while PdfStamper keeps as much of the original PDF as possible. 您的方法非常有损(它会丢弃文档级别的信息以及诸如表单字段之类的交互式部分),而PdfStamper会保留尽可能多的原始PDF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM