iTextSharp的插入PDF文件使用C#文本 [英] itextsharp insert text in pdf file with C#

查看:275
本文介绍了iTextSharp的插入PDF文件使用C#文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要插入的文本与使用code以下iTextSharp的PDF文件。很多时候,它的作品的权利,但其他时候它不能正常工作。

 的FileStream pdfOutputFile =新的FileStream(pdfTemplate,FileMode.Create);
PdfReader pdfReader =新PdfReader(pdffile,System.Text.Encoding.UTF8.GetBytes(ownerPassword));
PdfStamper pdfStamper = NULL;
// pdfReader.Permissions = 1;
pdfStamper =新PdfStamper(pdfReader,pdfOutputFile);
AcroFields TESTFORM = pdfStamper.AcroFields;PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(索引+ 1);
字符串[] = formattext printTxt.Split(新的char [] {'\\ n'});
浮lhight = 0;
浮abxt = ABX;printTxt =你好词;FT =新FormattedText(printTxt,Color.Black,宋体,EncodingType.Winansi,真实,9);
位图B =新位图(1,1);
显卡显卡= Graphics.FromImage(B);
字体F =新的字体(宋体,9);pdfPageContents.BeginText();BASEFONT BASEFONT = BaseFont.CreateFont(BaseFont.HELVETICA,ASCII,FALSE);
pdfPageContents.SetFontAndSize(BASEFONT,20); // 40点字体
pdfPageContents.SetRGBColorFill(0,0,0);
浮textAngle = 0;pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT,printTxt,ABX + 3,(浮点)奥比+ 12 + lhight,textAngle);
pdfPageContents.EndText();


解决方案

我用写上任何PDF文件中的文本的方法是,我使用的软件工具,PDF硝基专业创建文本字段(可以使用其他一些软件来创建这些字段)。一旦这样做,那么你可以用code以下方式写在这些领域的文本。

 字符串pdfTemplate =文件路径;
字符串NEWFILE = outputFilePath;
PdfReader PDFWriter =新PdfReader(pdfTemplate);
PdfStamper pdfStampDocument =新PdfStamper(PDFWriter,新的F​​ileStream(NEWFILE,FileMode.Create));
AcroFields pdfFormFields = pdfStampDocument.AcroFields;
//对于文本字段
pdfFormFields.SetField(txtTextFieldName,第一个文字);
//对于复选框栏
pdfFormFields.SetField(chkSomeCheckBox,是);
PDFWriter.Close();
pdfStampDocument.Close();

希望它帮助。

I want to insert text into a pdf file with iTextSharp using the code below. Many times it works right but other times it does not work.

FileStream pdfOutputFile = new FileStream(pdfTemplate, FileMode.Create);
PdfReader pdfReader = new PdfReader(pdffile, System.Text.Encoding.UTF8.GetBytes("ownerPassword"));
PdfStamper pdfStamper = null;
//   pdfReader.Permissions = 1;
pdfStamper = new PdfStamper(pdfReader, pdfOutputFile);
AcroFields testForm = pdfStamper.AcroFields;

PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(index + 1);
string[] formattext = printTxt.Split(new char[] { '\n' });
float lhight = 0;
float abxt = abx;

printTxt= "Hello word";

ft = new FormattedText(printTxt, Color.Black, "Arial", EncodingType.Winansi, true, 9);
Bitmap b = new Bitmap(1, 1);
Graphics graphics = Graphics.FromImage(b);
Font f = new Font("Arial", 9);

pdfPageContents.BeginText();

BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, "ASCII", false);
pdfPageContents.SetFontAndSize(baseFont,20); // 40 point font
pdfPageContents.SetRGBColorFill(0, 0, 0);
float textAngle = 0;

pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, printTxt, abx+3, (float)aby + 12 + lhight, textAngle);
pdfPageContents.EndText();

解决方案

The approach I use to write text on any Pdf file is that I create text fields using a software tool PDF Nitro Professional (You can use some other software to create these fields). Once done you can then use the following pattern of code to write text on those fields.

string pdfTemplate = filePath;
string newFile = outputFilePath;
PdfReader PDFWriter = new PdfReader(pdfTemplate);
PdfStamper pdfStampDocument = new PdfStamper(PDFWriter, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStampDocument.AcroFields;
//For Text field
pdfFormFields.SetField("txtTextFieldName", "First Text");
//For Check Box Field 
pdfFormFields.SetField("chkSomeCheckBox", "Yes");
PDFWriter.Close();
pdfStampDocument.Close();

Hope it helps.

这篇关于iTextSharp的插入PDF文件使用C#文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆