2012-04-10 3 views
0

아래 코드를 사용하여 iTextSharp로 PDF 파일에 텍스트를 삽입하고 싶습니다. 여러 번 제대로 작동하지만 다른 때는 작동하지 않습니다.itextsharp C#으로 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(); 
+0

[이] (http://stackoverflow.com/questions/3992617/itextsharp-insert-text-to-an-existing-pdf) –

+0

내 문제는 ..이 코드는 매번 작동하지 않습니다 ... 그것은 하나 또는 다른 pdf 파일에 작동하지 않는 –

답변

1

내가 어떤 PDF 파일에 텍스트를 작성하기 위해 사용하는 방법은 내가 소프트웨어 도구 PDF 니트로 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(); 

희망이 있습니다.

+0

위치에 pdf 파일에 서명자의 많은 서명 이미지, 날짜 및 서명자를 삽입하고 싶습니다 ... 서명 이미지 삽입 오른쪽하지만 텍스트 (이름, 날짜) 삽입하지 PDF 파일 –

+0

은 형식 문제 일 수 있습니다. 문서에서 필드를 만들 것을 제안하셨습니까? –

+0

ihave Doc를 pdf로 변환하고 텍스트를 삽입하십시오. 그것은 효과가있다. 하지만 이미지로 pdf를 만들고 그것에 텍스트를 삽입 할 때. 그것은 작동하지 않습니다 –