ITextSharp将文本插入到现有的pdf中

标题概括了所有内容。


我想使用iTextSharp将文本添加到现有的PDF文件中,但是我找不到如何在网络中的任何地方执行此操作...


PS:我不能使用PDF表格。


Qyouu
浏览 983回答 3
3回答

倚天杖

我找到了一种方法(不知道这是否是最好的方法,但它确实有效)string oldFile = "oldFile.pdf";string newFile = "newFile.pdf";// open the readerPdfReader reader = new PdfReader(oldFile);Rectangle size = reader.GetPageSizeWithRotation(1);Document document = new Document(size);// open the writerFileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);PdfWriter writer = PdfWriter.GetInstance(document, fs);document.Open();// the pdf contentPdfContentByte cb = writer.DirectContent;// select the font propertiesBaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252,BaseFont.NOT_EMBEDDED);cb.SetColorFill(BaseColor.DARK_GRAY);cb.SetFontAndSize(bf, 8);// write the text in the pdf contentcb.BeginText();string text = "Some random blablablabla...";// put the alignment and coordinates herecb.ShowTextAligned(1, text, 520, 640, 0);cb.EndText();cb.BeginText();text = "Other random blabla...";// put the alignment and coordinates herecb.ShowTextAligned(2, text, 100, 200, 0);cb.EndText();// create the new page and add it to the pdfPdfImportedPage page = writer.GetImportedPage(reader, 1);cb.AddTemplate(page, 0, 0);// close the streams and voilá the file should be changed :)document.Close();fs.Close();writer.Close();reader.Close();我希望这对某人==有用(并在此处发布任何错误)

慕运维8079593

除了上述出色的答案之外,以下内容还显示了如何向多页文档的每一页添加文本:&nbsp;using (var reader = new PdfReader(@"C:\Input.pdf"))&nbsp;{&nbsp; &nbsp; using (var fileStream = new FileStream(@"C:\Output.pdf", FileMode.Create, FileAccess.Write))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;var document = new Document(reader.GetPageSizeWithRotation(1));&nbsp; &nbsp; &nbsp; &nbsp;var writer = PdfWriter.GetInstance(document, fileStream);&nbsp; &nbsp; &nbsp; &nbsp;document.Open();&nbsp; &nbsp; &nbsp; &nbsp;for (var i = 1; i <= reader.NumberOfPages; i++)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.NewPage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var importedPage = writer.GetImportedPage(reader, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var contentByte = writer.DirectContent;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentByte.BeginText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentByte.SetFontAndSize(baseFont, 12);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var multiLineString = "Hello,\r\nWorld!".Split('\n');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var line in multiLineString)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, line, 200, 200, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentByte.EndText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentByte.AddTemplate(importedPage, 0, 0);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;document.Close();&nbsp; &nbsp; &nbsp; &nbsp;writer.Close();&nbsp; &nbsp; }&nbsp;}

尚方宝剑之说

这对我有用,包括使用OutputStream:PdfReader reader = new PdfReader(new RandomAccessFileOrArray(Request.MapPath("Template.pdf")), null);&nbsp; &nbsp; Rectangle size = reader.GetPageSizeWithRotation(1);&nbsp; &nbsp; using (Stream outStream = Response.OutputStream)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Document document = new Document(size);&nbsp; &nbsp; &nbsp; &nbsp; PdfWriter writer = PdfWriter.GetInstance(document, outStream);&nbsp; &nbsp; &nbsp; &nbsp; document.Open();&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PdfContentByte cb = writer.DirectContent;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb.BeginText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb.SetFontAndSize(BaseFont.CreateFont(), 12);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb.SetTextMatrix(110, 110);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb.ShowText("aaa");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb.EndText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PdfImportedPage page = writer.GetImportedPage(reader, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb.AddTemplate(page, 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; finally&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.Close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP