I'm generating a PDF file with MigraDoc. Now I need to add an image to the PDF, but if the image is bigger than the page, it is cut, as if part of it is outside the page. I tried to set the image width to a smaller size, but It's not working properly :
var sec = doc.AddSection();
var p = sec.AddParagraph();
var img = p.AddImage(imgFile);
img.Width = "10cm";
img.LockAspectRatio = true;
But in the result PDF, it seems its ignoring the width I'm setting, It appears with the same size it was before.
Found the solution, it was dumber then I thougt. I was testing in a small console applcation, but I was generating the pdf to "Result2.pdf" and opening "Result.pdf". Sorry. 😳
Related
I'm using ChromiumWebBrowser to convert a local html page that might have images in it to PDF in order to print it on a kiosk printer. The dimensions of the PDF should be 80mm x 200mm. The following is the code so far:
using (var browser = new ChromiumWebBrowser(url, browserSettings))
{
if (zoomLevel > 0)
{
browser.FrameLoadStart += (s, argsi) =>
{
var b = (ChromiumWebBrowser)s;
if (argsi.Frame.IsMain)
{
b.SetZoomLevel(zoomLevel);
}
};
}
await browser.WaitForInitialLoadAsync();
var contentSize = await browser.GetContentSizeAsync();
var viewport = new Viewport
{
Height = contentSize.Height,
Width = contentSize.Width,
Scale = 1.0
};
var printSettings = new PdfPrintSettings();
printSettings.PageWidth = 80000; // 80mm
printSettings.PageHeight = 200000; // 200mm
printSettings.BackgroundsEnabled = true;
await browser.PrintToPdfAsync("D:\\chromium_pdf.pdf", printSettings);
}
The html page is converted perfectly if it only contains text. Also, if it has images that fit within the specified print width and height, it converts fine. The issue occurs when large images are included in the html. This results in part of the image being cropped out from the right.
When printing to PDF manually in a normal browser, the popup that shows up has a tick box for "fit to printable area" that scales the entire page down to fit the whole image. I would like to simulate that through code.
In the PdfPrintSettings class, there is a property called ScaleFactor. When I change this from 100% to 50%, the image fits fine in the created PDF page. However, that 50% value is arbitrary and I would like to scale it based on the image.
I tried changing the viewport width and height or the browser zoom level but that does not affect the printed PDF.
I also don't want to take a screenshot of the PDF since that affects the print quality; printing text is much clearer.
How can I scale the page to fit into the PDF page using this ChromiumWebBrowser from CefSharp? Or how can I figure out what the ScaleFactor should be?
Here is a screenshot of the first and second pages of the PDF generated:
PrintToPdfAsync uses the same code path as https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
There is no specific support for fit to page. Until chromium adds support there won't be a built in option.
Possibly options to look at:
Add some css media print rules
Calculate scale based on content size.
I have code that creates a Word document using the OpenXml SDK. Part of the code adds an image to the front page of the document, and everything works perfectly. When I add code to include an image in the header as well, the front page image no longer displays--it doesn't even show a placeholder or a red x indicating the image is not found. The header image does display as expected.
I use the same function (BuildImage) to create the Drawing object in both cases. When building the header, if I comment out the line that appends that drawing, then the front page image remains. If I leave it in, then the front page image disappears.
Header h = new Header();
Paragraph p = new Paragraph();
Run r = new Run();
Drawing drawing = BuildImage(imageRelId, "logo_small.jpg", 150, 47);
r.Append(drawing); <-- This line causes the issue. If skipped, the image appears on front page
p.Append(r);
h.Append(p);
I have verified the document.xml, header.xml, all _rels files, etc after unzipping and everything looks fine to me. The image paths and media folder look good as well. If I do a comparison of the document.xml with and without the header image in the Word file, they are identical. I have no idea why the front page image will not show up.
I'm not sure if something changed in Word recently because everything did use to work using this code that has been untouched for over a year. This just started happening about a week ago.
i am creating pdf using synfusion pdf plugin for xamarin forms and what happenes if i print my pdf then there is many remaining space on the page so how i can get that remaining space of pdf page using syncfusion pdf
We can get the blank space region in the page of PDF document by using PdfLayoutResult. Please refer the below code snippet for more details,
C#:
//Create a text element with the text and font
PdfTextElement textElement = new PdfTextElement(text, font);
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;
//Draw the paragraph
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0,
page.GetClientSize().Width, page.GetClientSize().Height), layoutFormat);
//Get the blank space of PDF using PdfLayoutResult and page height
float blankSpaceHeight = page.GetClientSize().Height - result.Bounds.Bottom;
We have created the sample for the same which can be downloaded from below link,
Sample to get the remaining page size of PDF document
I am trying to create a PDF file by using EVOHTMLTOPDF. But the pdf generated is wrong and the contents has a font size so small that i cant even read it.
This is the current code I use inside a webservice
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
htmlToPdfConverter.HtmlViewerWidth = int.Parse("1024");
htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = EvoPdf.PdfPageSize.Letter;
htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = EvoPdf.PdfPageOrientation.Portrait;
htmlToPdfConverter.PdfDocumentOptions.TopMargin = 15f;
htmlToPdfConverter.PdfDocumentOptions.RightMargin = 15f;
htmlToPdfConverter.PdfDocumentOptions.BottomMargin = 15f;
htmlToPdfConverter.PdfDocumentOptions.LeftMargin = 15f;
byte[] outPdfBuffer = null;
outPdfBuffer = htmlToPdfConverter.ConvertHtml(DocContent, baseUrl);
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
File.WriteAllBytes(sourcepath, outPdfBuffer);
I have also attached a screenshot
Some time i get this error which reads "PDF is too big"
Please anybody provide a solution as i want to create a pdf and download the pdf to the users computer.
Could you try to see which is the minimum width of the browser window that can display the content of the HTML page you convert?
If the content width is very large it will be scaled down to fit the PDF page width. The solution would be to modify your HTML page to allow the content reflow at different browser window widths.
You can find a live demo where to test various scaling options at HTML to PDF Scaling Options Demo . For example you can check on 'Clip HTML Content Width' option to truncate the HTML content if it is too wide.
Regarding the "PDF is too big", when do you get this error? When you create the PDF, when you open the PDF in a viewer, etc. This does not seem to be an error from library.
To have font which set in html I made a formula:
htmlToPdfConverter.HtmlViewerWidth = 800 - (int)((htmlToPdfConverter.PdfDocumentOptions.RightMargin + htmlToPdfConverter.PdfDocumentOptions.LeftMargin) * 1.33);
After that font size in pdf from Evo converter is correct and equal to font size from pdf document which created with MS Word (export to pdf).
I'm building a PDF class with iTextSharp to handle my company's digital records. We use expensive paper that has columns and has our company's logo at the top. I've scanned a copy as a PDF and another as a JPG. I've read elsewhere that the IMG.Rotate() command should rotate the image. I've also read that Image.SetRotationDegrees(90) is supposed to work, but that line of code doesn't work for me. SetRotationDegrees isn't a method of Image. Here's what I've got so far:
string imageFilePath = _watermark;
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Rotate it for landscape
Image jpg.Rotate(); Image.GetInstance(System.Drawing.Image.FromStream(fs), ImageFormat.Jpeg);
//Scale image
jpg.ScalePercent(35f);
//Set position
jpg.ScaleToFit(770, 3000);
jpg.WidthPercentage = 100;
jpg.Rotate();
//Allignment
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
...
PDFReport.Add(jpg);
`
Anyone have any ideas? The image is being placed on the page but it places it portrait instead of landscape.