| By Date: | <-- --> |
| By Thread: | <-- --> |
|
Hello, Thanks for the great library for generating PDF! J I have a question about iTextSharp library: After some data
are bound to text fields, the paragraph font size is slightly changed. I
don’t know why? The attached screenshot shows a sample (Left side is PDF template,
right side is the generated PDF file). You can see “Sincerely” is
not in the same horizontal line. The attached “SampleJS.pdf” is the
PDF template. The main C# web site code is like below: -----------------------------------------------------------------------------------------------------
protected void BtnGetPDF_Click(object sender, EventArgs e)
{
// Get PDF stream
MemoryStream m = new MemoryStream();
CreatePDFStream(m);
// Get stream bytes
byte[] buffer =
m.GetBuffer();
// Save a copy to file for debug
purpose only
FileStream fs = new FileStream(Server.MapPath("testPDF.pdf"), FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
// Write pdf bytes to outputstream
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Accept-Header",
buffer.Length.ToString());
Response.AddHeader("Content-Length",
buffer.Length.ToString());
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.Flush();
Response.End();
}
/// <summary>
/// Create PDF stream
/// </summary>
private void CreatePDFStream(Stream m)
{
iTextSharp.text.pdf.PdfReader
pdfRd = new
iTextSharp.text.pdf.PdfReader(Server.MapPath("~/SampleJS.pdf")); iTextSharp.text.pdf.PdfStamper stamp = new iTextSharp.text.pdf.PdfStamper(pdfRd, m);
// Set form fields
// NOTE: It is supposed to get data
from database
iTextSharp.text.pdf.AcroFields
fields = stamp.AcroFields;
string formText =
fields.GetField("formText");
formText = formText.Replace("«Institution»",
txtInstitution.Text);
formText = formText.Replace("«AppID»",
txtAppID.Text);
formText = formText.Replace("«CurrentDate»",
DateTime.Now.ToString());
formText = formText.Replace("«Name»",
"John Nolan");
formText = formText.Replace("«AddressStreet»",
"123 test street");
formText = formText.Replace("«AddressCity»",
"
formText = formText.Replace("«AddressState»",
"MD");
formText = formText.Replace("«AddressZip»",
"20120");
formText = formText.Replace("«FirstName»",
"John");
formText = formText.Replace("«LoanType»",
txtLoanType.Text);
formText = formText.Replace("«PartnerURL»",
txtPartnerURL.Text);
fields.SetField("formText",
formText);
// Get content to make changes
PdfContentByte
overContent = stamp.GetOverContent(1); // Add logo image
iTextSharp.text.Image
logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/logo.jpg"));
float[] logoArea =
fields.GetFieldPositions("Logo");
iTextSharp.text.Rectangle
logoRect = new Rectangle(logoArea[1],
logoArea[2], logoArea[3], logoArea[4]);
// right alignment
logo.ScaleToFit(logoRect.Width, logoRect.Height);
logo.SetAbsolutePosition(logoArea[3] - logo.ScaledWidth + (logoRect.Width -
logo.ScaledWidth) / 2,
logoArea[2] + (logoRect.Height - logo.ScaledHeight) / 2);
overContent.AddImage(logo);
// Add Signature
iTextSharp.text.Image
signature = iTextSharp.text.Image.GetInstance(Server.MapPath("~/signature.jpg"));
float[] signArea =
fields.GetFieldPositions("Signature");
iTextSharp.text.Rectangle
signRect = new Rectangle(signArea[1],
signArea[2], signArea[3], signArea[4]);
signature.ScaleToFit(signRect.Width, signRect.Height);
signature.SetAbsolutePosition(signArea[1] + (signRect.Width -
signature.ScaledWidth) / 2,
signArea[2] + (signRect.Height - signature.ScaledHeight) / 2);
overContent.AddImage(signature);
//
Add barcode
Barcode39 code39 = new Barcode39();
code39.Code = txtAppID.Text;
code39.StartStopText = false;
iTextSharp.text.Image
image39 = code39.CreateImageWithBarcode(overContent, null, null); float[] barcodeArea =
fields.GetFieldPositions("AppIDBarCode");
iTextSharp.text.Rectangle
rect = new Rectangle(barcodeArea[1],
barcodeArea[2], barcodeArea[3], barcodeArea[4]);
image39.ScaleToFit(rect.Width, rect.Height); image39.SetAbsolutePosition(barcodeArea[1]
+ (rect.Width - image39.ScaledWidth) / 2,
barcodeArea[2] + (rect.Height - image39.ScaledHeight) / 2);
overContent.AddImage(image39);
stamp.FormFlattening = false;
stamp.FreeTextFlattening = true;
stamp.Close();
} ----------------------------------------------------------------------------------------------------- If you could figure out where the problem is, that would be
greatly appreciated. Jun Meng |
Attachment:
sample.jpg
Description: sample.jpg
Attachment:
SampleJS.pdf
Description: SampleJS.pdf
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ iText-questions mailing list iText-questions (at) lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions