Jpg to Pdf Convertor in C# 3.5 - c#

I need a method or library that convert a image jpg to file pdf. I tried find in web, but I did find only the SautinSoft, but this is to pdf to jpg and is not free.
Can someone help me?
This function is for C# 3.5.

What you need is a library to create PDFs, like http://www.pdfsharp.net/
Then you can create a pdf document and inject your image into it. You're not looking to convert a JPG to a PDF, that doesn't make much sense, you're trying to create a PDF with your JPG inside it.
Sample code from another answer: Overlay image onto PDF using PDFSharp
private void GeneratePDF(string filename, string imageLoc)
{
PdfDocument document = new PdfDocument();
// Create an empty page or load existing
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
DrawImage(gfx, imageLoc, 50, 50, 250, 250);
// Save and start View
document.Save(filename);
Process.Start(filename);
}
void DrawImage(XGraphics gfx, string jpegSamplePath, int x, int y, int width, int height)
{
XImage image = XImage.FromFile(jpegSamplePath);
gfx.DrawImage(image, x, y, width, height);
}

Related

Image loosing it's size iTextSharp

I am creating a PDF using iTextSharp and trying to add an image in the header of the PDF. The code is like this
public override void OnOpenDocument(PdfWriter writer, Document document)
{
image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/Content/images/HSBC/hsbc.png"));
image.ScaleAbsoluteHeight(25);
image.ScaleAbsoluteWidth(134);
image.SetAbsolutePosition(38, 800);
}
Note : The actual image size is height:25px and width:134px. That's what i am giving in the the image.ScaleAbsoluteHeight(25) and image.ScaleAbsoluteWidth(134) respectively.
But i don't understand why the image becomes large when PDF generated and it's look awful.
Thanks
You have to change your code like :
var imagepath = Server.MapPath("~/Images/logo.jpg");
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
image.SetAbsolutePosition(440, 10);
image.ScaleToFit(120f, 155.25f);
This will definitely work for you.
Cheers !!

Two images to one pdf file one image per page

I have two images and I want to convert it to one pdf file with one image in a page...
what is the easiest way of doing it in C#?
Edit: I tried the following code (added the PdfSharp reference):
string source1 = #"MySource1.JPG";
string source2 = #"MySource2.JPG";
string destinaton = #"MyDest.pdf";
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());
doc.Pages.Add(new PdfPage());
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(source1);
XGraphics xgr2 = XGraphics.FromPdfPage(doc.Pages[1]);
XImage img2 = XImage.FromFile(source2);
xgr.DrawImage(img, 0, 0);
xgr2.DrawImage(img2, 0, 0);
doc.Save(destinaton);
doc.Close();
Now it is create a pdf with my two pages but the problem now is that the images are cut and not in their original size! the size of the pictures is 3264x2448.
How can I fix the image size to the pdf document size?
There are several overloads of DrawImage. Use an overload that allows you to specify the destination size of the image.
Three lines of code allow to calculate the image size to use the complete page (with a margin if wanted) while keeping the aspect ratio.
If you know the pdf document size, you can resize your image as:
Bitmap objBitmap = new Bitmap(objImage, new Size(size1, size2));
where objImage is your original image.
or like:
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
objBitmap = resizeImage(objBitmap, new Size(size1,size2));

Why doesn't an Image show up when I add it to a Document using iTextSharp?

Contextt: I am opening an existing, interactive PDF form containing AcroForm fields. I tried to add an image to a rectangle field in the PDF form like this:
string path = HttpContext.Current.Server.MapPath("includes");
string newFile = HttpContext.Current.Server.MapPath("Tmp") + "/completed_gray" +".pdf";
string imagepath = HttpContext.Current.Server.MapPath("Tmp");
Document doc = new Document();
try {
PdfWriter.GetInstance(doc, new FileStream(newFile, FileMode.Open));
doc.Open();
iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(imagepath + "/CUstomRep_Eng_Col_1_V1.png");
iTextSharp.text.Rectangle rect = pdfStamper.AcroFields.GetFieldPositions("img_1_space")[0].position;
gif.ScaleAbsolute(rect.Width, rect.Height);
gif.SetAbsolutePosition(rect.Left, rect.Bottom);
doc.Add(gif);
}
catch (Exception ex) {
//Log error;
}
finally {
doc.Close();
}
The image doesn't show up in the resulting PDF.
You're creating a document using the "5 steps to create a PDF document" as documented in my books.
create a Document object.
create a PdfWriter instance.
open the document.
add content to the document.
close the document.
This contradicts with what you actually want to do: I want to add an Image in a placeholder defined by an AcroForm field.
Why are you saying you want one thing, and doing something else? Beats me. Probably because you didn't want to read the documentation.
You need something like this:
Create a PdfReader instance.
Create a PdfStamper instance.
Ask the stamper for information about the fields.
Add content to a page using the stamper instance.
Close the stamper.
In answer to your question: why doesn't my image show up in my document?
Support the coordinates of the field in the existing document are lower-left corner x = 600, y = 600 and upper-right corner x = 700, y = 700, then you are adding the image outside the visible area of the page you're creating. When you use new Document();, you're creating a document where the lower-left corner is x = 0, y = 0 and the upper-right corner is x = 595, y = 842.
In that case, you're adding the image to the document, but it's not visible because you've added it outside the rectangle that defines the page.

How to use graphics class to write onto a text file in c#

I need to create a document dynamically through code and then print and save it onto a .doc file. So far I have managed to use the graphics class to print the document but I have no idea how to get it to save the file in .doc or any text format for that matter. Is it possible to do this? If yes how can it be done?
I'm not sure this is what you are looking for, but if you want to save what you produce with Graphics on disk, you could use windows metafiles (wmf). If g is your instance of Graphics, something like this:
IntPtr hdc = g.GetHdc();
Rectangle rect = new Rectangle(0, 0, 200, 200);
Metafile curMetafile = new Metafile(#"c:\tmp\newFile.wmf", hdc);
Graphics mfG = Graphics.FromImage(curMetafile);
mfG.DrawString("foo", new Font("Arial", 10), Brushes.Black, new PointF(10, 10));
g.ReleaseHdc(hdc);
mfG.Dispose();
Assuming you don't really mean you want to save graphics as text, and just want to create a Word document then you need to look at Microsoft.Office.Interop.Word.
i.e. from DotNetPearls:
using System;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
// Open a doc file.
Application application = new Application();
Document document = application.Documents.Open("C:\\word.doc");
// Loop through all words in the document.
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// Write the word.
string text = document.Words[i].Text;
Console.WriteLine("Word {0} = {1}", i, text);
}
// Close word.
application.Quit();
}
}

ITextSharp Defining a Pages Background Image: Whats the correct way?

Is it possible to set a background image for a pdf page in ITextSharp?
Whats the correct way to define a background image for a pdf page? Is there a property of the document I set?
Or is it just like creating another image(my image has the dimensions of a A4 page)? If I add the background image as a normal image will I be able to place paragraphs over the top of the background image?
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWrite object, writing the output to a MemoryStream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/test.jpg"));
logo.SetAbsolutePosition(0,0);
document.Add(logo);
// Will the following paragraph be ON TOP or below the background image?
// I am aiming for on top
document.Add( new Paragraph("sjfkkjdsfk") );
document.Close();
The logo is fine - it's just a regular image content on the page, not backgound img (like in HTML). To put content on top of that, you need to use Direct Content:
PdfContentByte over = writer.DirectContent;
over.SaveState();
over.BeginText();
over.SetFontAndSize(BaseFont.CreateFont(), 9);
over.ShowTextAligned(Element.ALIGN_LEFT, "Your Text", x, y, 0);
over.SetLineWidth(0.3f);
over.EndText();
over.RestoreState();
Please note the x and y cord are from the bottom left corner. The last parameter is for rotation.
Another tip: do a doc.NewPage(); if you want to start a new page with the image background, so the the text cord is on the new page.

Categories