我正在尝试从Windows窗体以pdf格式创建报告,因此我使用 itextsharp库来创建pdf格式,如下所示:
Document reportaspdf = new Document(iTextSharp.text.PageSize.LETTER, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(reportaspdf, new FileStream(sfd.FileName, FileMode.Create));
reportaspdf.Open();
#region Design border
var content = writer.DirectContent;
var pageBorderRect = new iTextSharp.text.Rectangle(reportaspdf.PageSize);
pageBorderRect.Left += reportaspdf.LeftMargin;
pageBorderRect.Right -= reportaspdf.RightMargin;
pageBorderRect.Top -= reportaspdf.TopMargin;
pageBorderRect.Bottom += reportaspdf.BottomMargin;
content.SetColorStroke(BaseColor.BLACK);
content.Rectangle(pageBorderRect.Left, pageBorderRect.Bottom, pageBorderRect.Width, pageBorderRect.Height);
content.Stroke();
#endregion
//Add Text
var TitleFont = FontFactory.GetFont("Segoe UI", 50.0f, BaseColor.BLACK);
var SubtitleFont = FontFactory.GetFont("Segoe UI", 30.0f, BaseColor.BLACK);
var DescribtionFont = FontFactory.GetFont("Segoe UI", 20.0f, BaseColor.ORANGE);
var p1 = new Paragraph("Final Report", TitleFont);
p1.Alignment = Element.ALIGN_CENTER;
reportaspdf.Add(new Paragraph(p1));
#region add originalimg
var Describitiontext = new Paragraph("Original Image", DescribtionFont);
Describitiontext.Alignment = Element.ALIGN_CENTER;
Describitiontext.SpacingBefore = 20;
reportaspdf.Add(new Paragraph(Describitiontext));
iTextSharp.text.Image orignamIMG = iTextSharp.text.Image.GetInstance(OrignalImage, System.Drawing.Imaging.ImageFormat.Jpeg);
orignamIMG.Alignment = Element.ALIGN_CENTER;
reportaspdf.Add(orignamIMG);
#endregion
到现在为止,一切都非常完美,但是在添加了其他图像和文本之后,它们已经被添加到先前的图像之下,但是我想将我的报告构造为每2个图像处于同一级别,依此类推
那么,如何添加同一级别的多个项目?
第二件事是 当我从面板上进行屏幕截图时,图像的宽度变得大于文档的宽度,因此我使用此功能来调整图像的大小,然后再将其添加到pdf文档中,但是分辨率却很差,
那么,如何在不降低画质的情况下调整图像大小?
public Bitmap Resizeimage(Bitmap image, int maxWidth, int maxHeight)
{
// Get the image's original width and height
int originalWidth = image.Width;
int originalHeight = image.Height;
// To preserve the aspect ratio
float ratioX = (float)maxWidth / (float)originalWidth;
float ratioY = (float)maxHeight / (float)originalHeight;
ratio = Math.Min(ratioX, ratioY);
// New width and height based on aspect ratio
int newWidth = (int)(originalWidth * ratio);
int newHeight = (int)(originalHeight * ratio);
// Convert other formats (including CMYK) to RGB.
Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
// Draws the image in the specified size with quality mode set to HighQuality
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}