Convert Image file to PDF document letter size using PDFTron - c#

I am trying to convert an image file to a PDF document with a defined page size (letter size).
Currenlty I am able to convert an image to a PDF document without defining any page dimensions (default dimensions of the PDF is the image size). I would like to define the page dimensions on the creation of the document, and place the image on that page (possibly with margins).
The following code snippet shows how I am currently converting an image file to a PDF document without setting any dimensions for the page:
async static Task<bool> ConvertImageToPDF(TestFile file)
{
pdftron.PDF.PDFDoc pdfdoc = new PDFDoc(); //Initialize a new PDF document
pdftron.PDF.Convert.ToPdf(pdfdoc, file.InputFile); //Use the Convert.ToPdf to generate the file from the image file
await pdfdoc.SaveAsync(file.OutputFile, SDFDocSaveOptions.e_linearized); //Save the PDF document generated from the conversion
pdfdoc.Destroy();
pdfdoc = null;
}
Any help or direction for assigning dimensions (letter size) to a PDF page and inserting the image file in that page would be more than welcome.

If ToPDF is given an image then PDFNet will query the DPI information of the image metadata and make page dimensions to match the DPI and resolution of the source.
If you like, you can always post-process the PDF generated by ToPDF.
Or, you can follow the AddImage sample code to do everything yourself.
https://www.pdftron.com/pdfnet/samplecode.html#AddImage

Related

Copy and resize a document and fit it into box

I'm using PDFsharp to create a PDF.
I am trying to copy another document/image (PDF format with A3 size) and paste it within the image box, in the new document (A4 size).
In the new document, there would be an image details box and an image box.
So, how do I copy the image from another PDF into the image box in the new document?
Below is the sample I need to create using PDFsharp.
You can draw pages from other PDF files like you draw images - draw them anywhere at any angle and any size.
You may have to do some calculations to maintain the aspect ratio.
See the Two Pages on One sample:
http://pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx
Lines 40 and 54 draw PDF pages. Check the lines above for prerequisites.

how to convert html file containing SVG (Scalable Vector Graphics) images to pdf file using ironPdf in C#?

I am trying to convert HTML page to pdf. Html page contains css, js code with svg images. I used wkhtmltopdf, iTextSharp and IronPdf dlls to get the exact html page into pdf. Functionality present in each of the above dll gives me pdf file containing content of html,css and js but excludes svg images.
Is there any way to render the svg images into pdf.
I am pasting code snippet here:
HtmlToPdf HtmlToPdf = new HtmlToPdf();
HtmlToPdf.PrintOptions.EnableJavaScript = true;
HtmlToPdf.PrintOptions.CssMediaType =PdfPrintOptions.PdfCssMediaType.Screen;
PdfResource PDF = HtmlToPdf.RenderUrlAsPdf(new Uri(htmlFilePath));
PDF.SaveAs(pdfFilePath);
IronPDF extract SVG images, To convert html page containing SVG images we need to set height with some values on SVG tag. If we did not mention height in SVG tag it will be considered as default i.e. to 0px.
This can be achieved using style= height:400px in SVG tag.

Windows API Code Pack - ShellFile not generating PDF bitmap

Using the code from previous stack overflow questions:
System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);
image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);
I am trying to use window API to get the thumbnail of a PDF
I am led to believe that this generates an image file that resembles the first page of the PDF document.
The reality however is that it does NOT look like that, and merely looks like the PDF icon.
Is there anything that I'm missing that is required before this actually works as intended?
PDF files are correctly associated with adobe reader.
When browsing directories in windows explorer I DO see thumbnails associated with the documents.
I should note that the code DOES in fact correctly extract thumbnails when dealing with Excel and Word documents.
EDIT (references):
C# get thumbnail from file via windows api
Get thumbnail of any file, not only image files on Windows XP/Vista
Windows API Code Pack Thumbnail gives preview thumb of pdf but not Word or Excel
You need to specify that you want the thumbnail, not the icon (the default).
Change your code into this:
System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);
//force the actual thumbnail, not the icon
f.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly;
image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);
The problem is because you have not selected the active frame that you will create the thumbnail from.
I cannot verify it on my current machine, because I don't have the Windows API on it, but it's giving you the standard PDF thumbnail because in your code you have't specified which page to use for the thumbnail.
Try doing something like this:
Image image = new Image();
//Load image here
int frameindex = 1; // Page number you want to use for thumbnail
Guid guide = image.FrameDimensionsList[0];
FrameDimension fDimension = new FrameDimension(guide);
image.SelectActiveFrame(fDimension, frameindex);
//Then go on to extract your thumbnail
I was not able to get ExtraLargeBitmap to work for PDF files but all other sizes (Large, Medium, and Small) worked fine.
Dim MyShellFile As ShellFile = ShellFile.FromFilePath(fi.FullName)
Dim MyThumbNail As Image
MyShellFile.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly
MyThumbNail = MyShellFile.Thumbnail.LargeBitmap
Me.PictureBox2.Image = MyThumbNail

How to convert Text containing Images into pdf using iText#

I have a richtextbox and by far, i have been successful in converting plain text into pdf using iTextsharp. Now, the situation is, when i copy some text from any source let's say a website and along text, it contains images. Now when i try to convert the content (text + images) into pdf, resulting pdf doesn't show images. I know that there would be some property to be set for the richtextbox or using itextsharp so to have images as well. I also know that we can insert images by giving path to that image but this is not what i want. I want to have plain text along with images while direct conversion from richtextbox to pdf. Forexample i have,![Im resulting pdf i want the same as in richtextbox][1]: http://i.stack.imgur.com/ts0ec.jpg
How can i have the same as in richtextbox? How can i specify orientation for text and image to be justified?

abcPDF is malforming my image after adding it to a pdf

I'm getting a byte array representing a TIFF file from my server, converting it into an XImage, and then adding it to a new pdf document. The image in question is 1280x800 (it was a screenshot) and is being stretched out and lengthened to fit the height and width of a standard pdf page. How do I fix this?
Try this:
doc.MediaBox.String = img.BoundingBox.String;
doc.Rect.String = doc.MediaBox.String;
where doc is Doc and img is XImage
The particular problem you have is due to having the printer set to Portrait.
Being a PDF object, you may need to emulate a page that mimics the image size you are after.
You might be better off sending your original document to an image and adding your TIFF file.
Depends on how the PDF page is being built. What you probably want to do is ensure that the PDF page that you are creating has the same aspect ratio as your image rather than defaulting to a letter sized page.
Since you don't specify how you're making the PDF page, I can't help you specifically. But in general, PDF pages are in units of 1/72 of an inch. You can figure the right size page like this:
const kPdfPageUnitSize = 72f;
float GetPdfSize(int nPixels, float dotsPerInch) {
return (nPixels / dotsPerInch) * kPdfPageUnitSize;
}
// later
float pdfwidth = GetPdfSize(image.Width, image.Resolution.X); // or however you have this
float pdfheight = GetPdfSize(image.Height, image.Resolution.Y);
As I can see, image scale coefficient is incorrect. You should divide image Height and image Width on 1,33(3) ( i.e. 96f/72f )
By default PDF user unit is 1⁄72 inch, so your image DPI should be proportional with original.

Categories