Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C在pdf文件中插入文本#_C#_Asp.net_Itextsharp - Fatal编程技术网

C# 使用C在pdf文件中插入文本#

C# 使用C在pdf文件中插入文本#,c#,asp.net,itextsharp,C#,Asp.net,Itextsharp,我想使用下面的代码将文本插入到带有iTextSharp的pdf文件中。很多时候它工作正常,但其他时候它不工作 FileStream pdfOutputFile = new FileStream(pdfTemplate, FileMode.Create); PdfReader pdfReader = new PdfReader(pdffile, System.Text.Encoding.UTF8.GetBytes("ownerPassword")); PdfStamper pdfStamper =

我想使用下面的代码将文本插入到带有iTextSharp的pdf文件中。很多时候它工作正常,但其他时候它不工作

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();

我在任何Pdf文件上写入文本的方法是使用软件工具Pdf Nitro Professional创建文本字段(您可以使用其他软件创建这些字段)。完成后,您可以使用以下代码模式在这些字段上编写文本

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();

希望有帮助。

我的问题是..此代码不是每次都有效…它在一个pdf文件上有效或不在另一个pdf文件上有效我想在一个位置的pdf文件中插入许多签名图像、签名者的日期和姓名…签名图像插入正确,但pdf文件中未插入的文本(名称、日期)可能是格式问题。您是否尝试过在文档中创建字段的建议?我已将文档转换为pdf并插入文本。它正在工作。但当创建带有图像的pdf并在其上插入文本时。它不工作,我有两个图像。我把它们转换成Pdf文件。现在我想使用下面的代码插入带有ItestSharp的文本。但是文本没有插入。嘿,问题解决了。我使用的是PdfStamper.GetunderContent,而不是GetOvercontent。它起作用了。谢谢大家的帮助。。。