Создание организованного отчета в формате PDF с использованием itextsharp - PullRequest
0 голосов
/ 09 ноября 2018

Я пытаюсь создать отчет из моей формы 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...