我正在使用iTextSharp将文本添加到PDF文件中。文本正确对齐。 问题是当我打印时,文字并不像我想要的那样清晰。 我在这做错了什么? 我在C#中使用WinForms。
using iTextSharp.text;
using iTextSharp.text.pdf;
/// <summary>
/// Add a page containing a single image.
/// Set the page size to match the image size.
/// </summary>
/// <param name="doc"></param>
private void AddPageWithImage(iTextSharp.text.Document doc, String imagePath)
{
Bitmap bmp = new Bitmap(612, 792);
bmp.SetResolution(72,72);
using (Graphics graph = Graphics.FromImage(bmp))
{
System.Drawing.Rectangle ImageSize = new System.Drawing.Rectangle(0, 0, 612, 792);
graph.FillRectangle(Brushes.White, ImageSize);
}
int totalRecords = Int32.Parse(comboBoxNumberOfGunPermits.SelectedItem.ToString());
int y = 0;
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
StringFormat format = new StringFormat();
//format.LineAlignment = StringAlignment.Center;
format.Alignment = StringAlignment.Center;
StringFormat leftAlign = new StringFormat();
//format.LineAlignment = StringAlignment.Center;
leftAlign.Alignment = StringAlignment.Near;
System.Drawing.Font mFont = new System.Drawing.Font("Microsoft Sans Serif", 8);
int difference = 175;
for (int i = 1; i <= totalRecords; i++)
{
g.DrawString("SOME TEXT", mFont, Brushes.Black, new RectangleF(70, 95 + y, 159, 408), format);
y += difference;
}
g.Flush();
string path = System.IO.Path.GetTempPath() + "/tmp.bmp";
bmp.Save(path);
//System.Diagnostics.Process.Start("mspaint", path);
// Read the image file
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(path);
// Set the page size to the dimensions of the image BEFORE adding a new page to the document
// Pad the height a bit to leave room for the page header
float imageWidth = image.Width;
float imageHeight = image.Height;
doc.SetMargins(0, 0, 0, 0);
doc.SetPageSize(new iTextSharp.text.Rectangle(imageWidth, imageHeight + 100));
doc.Add(image);
image = null;
}