Creating an organized report as a pdf using itextsharp
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
c# winforms pdf bitmap itext
edited Nov 11 at 1:05
kennyzx
9,64542263
9,64542263
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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