Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm developing windows application to generate pdf with the help of pdfsharp.dll file.I want to generate a image in pdf.While adding the image,i got an error like
The given path's format is not supported.

My code is
XImage img = XImage.FromFile("‪C:/Users/D3/Desktop/img1.jpg");
Posted
Updated 28-Mar-19 5:25am

string pdfpath = Server.MapPath("PDFs");
string imagepath = Server.MapPath("Images");
Document doc = new Document();
try
{
PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Images.pdf", FileMode.Create));
doc.Open();

doc.Add(new Paragraph("GIF"));
Image gif = Image.GetInstance(imagepath + "/mikesdotnetting.gif");
doc.Add(gif);
}
catch (Exception ex)
{
//Log error;
}
finally
{
doc.Close();
}
 
Share this answer
 
Comments
Richard MacCutchan 24-Dec-14 10:06am    
Please format your code.
SofiaRodrigues 28-Mar-19 11:20am    
The code is from iTextSharp and Divakard3 was asking for pdfsharp.dll!
This code works well with TIFF files!
Your solution can be something like this:

string name = @"path\of\your\image";
string dest = @"path\of\destination";
// each source file separate
PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument();

XImage img = XImage.FromFile(name);
img.Interpolate = false;
int width = img.PixelWidth;
int height = img.PixelHeight;
PdfSharp.Pdf.PdfPage page = new PdfSharp.Pdf.PdfPage
{
    Width = width,
    Height = height
};
doc.Pages.Add(page);
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);

xgr.DrawImage(img, 0, 0, width, height);
img.Dispose();
xgr.Dispose();
//  save to destination file
FileInfo fi = new FileInfo(name);

doc.Save(dest + "\\" + name + ".PDF");
doc.Dispose();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900