Creating an organized report as a pdf using itextsharp









up vote
1
down vote

favorite
1












I am trying to create a report from my windows form as a pdf, So i used the itextsharp library to create the pdf like that:



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


Until now everything is perfect but after adding another images and text they have been added under the previous images but i want to structure my report as every 2 image at the same level and so on



So, How can i added more than one item at the same level ?



The second thing is
when i take a screen capture from panel, the width of image became larger than the document width so i use this function to resize the image before adding it to the pdf document but it's resolution become so poor,



So, How can i resize image without loosing the quality ?



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;










share|improve this question























  • Welcome to SO! I suggest you ask the 2nd question in another post, which makes the questions easier to answer and follow, you can add links in the questions to reference each other to provide context.
    – kennyzx
    Nov 11 at 1:09














up vote
1
down vote

favorite
1












I am trying to create a report from my windows form as a pdf, So i used the itextsharp library to create the pdf like that:



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


Until now everything is perfect but after adding another images and text they have been added under the previous images but i want to structure my report as every 2 image at the same level and so on



So, How can i added more than one item at the same level ?



The second thing is
when i take a screen capture from panel, the width of image became larger than the document width so i use this function to resize the image before adding it to the pdf document but it's resolution become so poor,



So, How can i resize image without loosing the quality ?



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;










share|improve this question























  • Welcome to SO! I suggest you ask the 2nd question in another post, which makes the questions easier to answer and follow, you can add links in the questions to reference each other to provide context.
    – kennyzx
    Nov 11 at 1:09












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I am trying to create a report from my windows form as a pdf, So i used the itextsharp library to create the pdf like that:



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


Until now everything is perfect but after adding another images and text they have been added under the previous images but i want to structure my report as every 2 image at the same level and so on



So, How can i added more than one item at the same level ?



The second thing is
when i take a screen capture from panel, the width of image became larger than the document width so i use this function to resize the image before adding it to the pdf document but it's resolution become so poor,



So, How can i resize image without loosing the quality ?



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;










share|improve this question















I am trying to create a report from my windows form as a pdf, So i used the itextsharp library to create the pdf like that:



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


Until now everything is perfect but after adding another images and text they have been added under the previous images but i want to structure my report as every 2 image at the same level and so on



So, How can i added more than one item at the same level ?



The second thing is
when i take a screen capture from panel, the width of image became larger than the document width so i use this function to resize the image before adding it to the pdf document but it's resolution become so poor,



So, How can i resize image without loosing the quality ?



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;







c# winforms pdf bitmap itext






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 1:05









kennyzx

9,52042263




9,52042263










asked Nov 9 at 20:09









user10630625

52




52











  • Welcome to SO! I suggest you ask the 2nd question in another post, which makes the questions easier to answer and follow, you can add links in the questions to reference each other to provide context.
    – kennyzx
    Nov 11 at 1:09
















  • Welcome to SO! I suggest you ask the 2nd question in another post, which makes the questions easier to answer and follow, you can add links in the questions to reference each other to provide context.
    – kennyzx
    Nov 11 at 1:09















Welcome to SO! I suggest you ask the 2nd question in another post, which makes the questions easier to answer and follow, you can add links in the questions to reference each other to provide context.
– kennyzx
Nov 11 at 1:09




Welcome to SO! I suggest you ask the 2nd question in another post, which makes the questions easier to answer and follow, you can add links in the questions to reference each other to provide context.
– kennyzx
Nov 11 at 1:09

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232667%2fcreating-an-organized-report-as-a-pdf-using-itextsharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232667%2fcreating-an-organized-report-as-a-pdf-using-itextsharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto