I want to create a portrait gallery with some photos. In order to achieve this, I add everything I need in a panel and then I get the visual of this panel to add it in a PDF.
My problem is that I managed to create the PDF containing the image, but it is bad quality.
Example 1
Example 2
We see that the outline of peoples is not clear, while in the original photos the outline is very clear.
iTextSharp.text.Document doc = new iTextSharp.text.Document();
panelTombi.AutoScrollPosition = new Point(0, 0);
panelTombi.AutoSize = true;
panelTombi.Refresh();
int width = panelTombi.Width;
int height = panelTombi.Height;
Bitmap MemoryImage = new Bitmap(width, height);
panelTombi.DrawToBitmap(MemoryImage, new System.Drawing.Rectangle(0, 0, width, height));
iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance((System.Drawing.Image)MemoryImage, System.Drawing.Imaging.ImageFormat.Jpeg);
image1.ScaleToFit(doc.PageSize);
image1.SetAbsolutePosition(0, PageSize.A4.Height - image1.ScaledHeight - doc.TopMargin);
MemoryImage.Save(Path.GetDirectoryName(sfd.FileName) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(sfd.FileName) + ".jpeg");
//Save pdf file
PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
doc.Open();
doc.Add(image1);
doc.Close();
As you can see, the image is also saved.
Could you tell me how to fix this image quality problem on the PDF file?
Thank you
Related
I am generating a byte array image and in order to convert it to pdf I am adding that image into a PDF document. the image size is exactly 812, 1015 DPI and even though I have document with the same size the image is being offset by about an inch (the red bar represents this offset) because of this I am missing about the same amount on the other side. Why is the PDF adding the image in this way. Here is the code:
var resizedImage = new Bitmap(812, 1015);
var drawResizedImage = Graphics.FromImage(resizedImage);
drawResizedImage.DrawImage(img, 0, 0, 812, 1015);
resizedImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
return getPDFDocument(memoryStream);
private byte[] getPDFDocument(MemoryStream inputImageStream)
{
MemoryStream workStream = new MemoryStream();
iTextSharp.text.Document document = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(812, 1015));
PdfWriter.GetInstance(document, workStream).CloseStream = false;
document.Open();
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(inputImageStream.ToArray());
document.Add(pdfImage);
document.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return workStream.ToArray();
}
This is producing this
Add this line to your code block
pdfImage.SetAbsolutePosition(0, 0);
You should see this:
document.Open();
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(inputImageStream.ToArray());
pdfImage.SetAbsolutePosition(0, 0);
document.Add(pdfImage);
document.Close();
That should give you the desired positioning.
Im doing a image to pdf program.
I want to set the size of the image as the size of the pdf and a extra space on top with 50
i tried this code
using (var imageStream = new FileStream(imagelocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var image = Image.GetInstance(imageStream);
Document document = new Document(new Rectangle(image.Width, image.Height), 0, 0, 0, 0);
using (var stream = new FileStream(pdfOutput, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
document.Add(image);
document.Close();
}
}
but the problem is it doesn't have a top margin,
when i try this code
Document document = new Document(new Rectangle(image.Width, image.Height), 0, 50, 50, 0);
it crop a part of the image for the space. how can i make this work?
By setting the document margins like "0, 50, 50, 0" you are also setting a margin-right, that might not be what you want in this case. The margin top is the correct way to get some space above the image.
Alternatively you can add some empty lines to your document before adding the image, e.g.: document.Add(new Paragraph(Chunk.NewLine))
You can use image.ScaleToFit() and pass in something like document.PageSize.Height and document.PageSize.Width to make that image fit your page.
this is my solution. i didn't realize that this simple code will work
Document document = new Document(new Rectangle(image.Width, image.Height + 150), 0, 0, 150, 0);
I need to render a BMP to a PDF using Aspose and download it.
The downloaded PDF is corrupt and Adobe will not open it. The saved to file PDF is fine and opens. Anyone know why the PDF from download is corrupted?
protected void ASPxButton1_OnClick(object sender, EventArgs e)
{
WebsiteToImage websiteToImage = new WebsiteToImage(HttpContext.Current.Request.Url.AbsoluteUri, #"C:\Temp\Test.jpg");
var generate = websiteToImage.Generate();
// Create a MemoryStream object from image Byte array
MemoryStream ms = new MemoryStream();
websiteToImage.Bitmap.Save(ms, ImageFormat.Jpeg);
// create a PDF object
Pdf pdf = new Pdf();
// create a section and add it to pdf document
Aspose.Pdf.Generator.Section MainSection = pdf.Sections.Add();
//Add the radio form field to the paragraphs collection of the section
// create an image object
Aspose.Pdf.Generator.Image sample_image = new Aspose.Pdf.Generator.Image();
// specify the image file path information
//sample_image.ImageInfo.File = #"d:/pdftest/untitled.bmp";
sample_image.ImageInfo.ImageStream = ms;
// specify the image file type
sample_image.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Bmp;
// specify the image width information equal to page width
sample_image.ImageInfo.FixWidth = MainSection.PageInfo.PageWidth - MainSection.PageInfo.Margin.Left - MainSection.PageInfo.Margin.Right;
// specify the image Height information equal to page Height
sample_image.ImageInfo.FixWidth = MainSection.PageInfo.PageHeight - MainSection.PageInfo.Margin.Top - MainSection.PageInfo.Margin.Bottom;
// create bitmap image object to load image information
Bitmap myimage = websiteToImage.Bitmap;
// check if the width of the image file is greater than Page width or not
if (myimage.Width > MainSection.PageInfo.PageWidth)
// if the Image width is greater than page width, then set the page orientation to Landscape
MainSection.IsLandscape = true;
else
// if the Image width is less than page width, then set the page orientation to Portrait
MainSection.IsLandscape = false;
// add image to paragraphs collection of section
MainSection.Paragraphs.Add(sample_image);
// save the resultant PDF
pdf.Save(#"C:\Temp\Test.pdf");
pdf.Save(ms);
byte[] bytes = ms.GetBuffer();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + Operator.Emplid + "Gudiance.pdf");
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();
ms.Close();
It appears that you are using a legacy Aspose.Pdf.generator approach, so in order to accomplish your requirements, we recommend you to please try using new Document Object Model of Aspose.Pdf namespace. Please try using following code snippet.
// create a PDF object
Document pdf = new Document();
// create a section and add it to pdf document
Aspose.Pdf.Page MainSection = pdf.Pages.Add();
//Add the radio form field to the paragraphs collection of the section
// create an image object
Aspose.Pdf.Image sample_image = new Aspose.Pdf.Image();
// specify the image file path information
sample_image.File = #"C:\pdftest\OutOfMemoryDemo\OutOfMemoryDemo\Sample.bmp";
// specify the image width information equal to page width
sample_image.FixWidth = MainSection.PageInfo.Width - MainSection.PageInfo.Margin.Left - MainSection.PageInfo.Margin.Right;
// specify the image Height information equal to page Height
sample_image.FixWidth = MainSection.PageInfo.Height - MainSection.PageInfo.Margin.Top - MainSection.PageInfo.Margin.Bottom;
// create bitmap image object to load image information
System.Drawing.Bitmap myimage = new System.Drawing.Bitmap(#"C:\pdftest\OutOfMemoryDemo\OutOfMemoryDemo\Sample.bmp");
// check if the width of the image file is greater than Page width or not
if (myimage.Width > MainSection.PageInfo.Width)
// if the Image width is greater than page width, then set the page orientation to Landscape
MainSection.PageInfo.IsLandscape = true;
else
// if the Image width is less than page width, then set the page orientation to Portrait
MainSection.PageInfo.IsLandscape = false;
// add image to paragraphs collection of section
MainSection.Paragraphs.Add(sample_image);
MemoryStream stream = new MemoryStream();
// save the resultant PDF
pdf.Save(stream);
StreamReader reader = new System.IO.StreamReader(Request.InputStream);
String Data = reader.ReadToEnd();
this.Title = Data;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.AddHeader("Accept-Header", stream.Length.ToString());
Response.AddHeader("Content-Length", stream.Length.ToString());
Response.AddHeader("Expires", "0″");
Response.AddHeader("Pragma", "cache");
Response.AddHeader("Cache-Control", "private");
Response.AddHeader("content-disposition", String.Format("inline;filename={0}", "article" + "docId"+ ".pdf"));
Response.ContentType = "application/pdf";
Response.AddHeader("Accept-Ranges", "bytes");
Response.BinaryWrite(stream.ToArray());
Response.Flush();
Response.End();
PS, My name is Nayyer and I am developer evangelist at Aspose.
I have a PDF of a blank 1-page MS Word doc. I want to add a text box to it. Code as follows:
void addAnnotation()
{
string filename = #"C:\Users\userID\Documents\Visual Studio 2013\Projects\PDFConverterTester\TestAddSrc.pdf";
string destFile = #"C:\Users\userID\Documents\Visual Studio 2013\Projects\PDFConverterTester\TestAddDest.pdf";
PdfReader pdfReader = new PdfReader(filename);
FileStream stream = new FileStream(destFile, FileMode.Create);
PdfStamper pdfStamper = new PdfStamper(pdfReader, stream);
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(100,100, 100, 100);
PdfWriter pdfWriter = pdfStamper.Writer;
PdfAnnotation annot = PdfAnnotation.CreateStamp(pdfWriter, rect, "XYZABC add rect", "new rectangle");
pdfStamper.AddAnnotation(annot, 1);
pdfStamper.Close();
}
When I open TestAddDest.pdf, it's blank, just like the source PDF, and the file size is the same, so I'm assuming the annotation has not been added. How can I add this text box?
Edit:
Source PDF: http://docdro.id/jAOpxr3
Destination PDF: http://docdro.id/gOaHsQm
The reason the PDF is blank is because the Rectangle is zero width and zero height :
iTextSharp.text.Rectangle rect = new
iTextSharp.text.Rectangle(100,100, 100, 100);
Put a breakpoint on that line or:
Console.WriteLine("W: {0}, H: {1}", rect.Width, rect.Height);
output:
W: 0, H: 0
Try something like this, which places the Rectangle at the top of the page:
Rectangle rect = new Rectangle(20, 700, 400, 776);
Destination PDF:
i am a new to c# programming and i am doing a project that can capture the screen and attach it to a pdf,in my project i need to attach an screenshot of my desktop or application directly to PDF file, i know how to attached a image that has been saved in to the Hard Drive, in this case i used iTextsharp library. what i need to know is there a way to attach the image directly to PDF without saving the image to Hard Drive. till now I can capture my screen and save it to the hard drive. please direct me to a path, Thanx In Advance , code i used is bellow
this is the code i used to capture the screen
string pHotoTime = DateTime.Now.ToString("yyyy-MM-dd, HH mm ss");
Size s = Screen.PrimaryScreen.Bounds.Size;
Bitmap bmp = new Bitmap(s.Width, s.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 90, s);
System.IO.Stream stream = new System.IO.MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
// later:
//bmp.Save(#"C:\Users\NiyNLK\Documents\TESTs\" + pHotoTime + ".jpg");
bmp.Save(#"C:\Users\NiyNLK\Documents\TESTs\MyImage.jpg");
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = bmp;
below code is used to attached the image
enter code here
iTextSharp.text.Rectangle r = new iTextSharp.text.Rectangle(400, 400);
var doc = new Document(r);
string path = #"c:\users\niynlk\documents\tests";
string Imagepath = #"C:\Users\NiyNLK\Documents\TESTs";
try
{
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path + "/Images.pdf", FileMode.Create));
doc.Open();
doc.Add(new Paragraph("Image"));
Image img = Image.GetInstance(Imagepath + "/MyImage.jpg");
img.ScalePercent(25f);
doc.Add(img);
}
catch (Exception)
{
throw;
}
finally
{
doc.Close();
}