I have a PDF generated by a third party with incorrect dimensions. They need to be both wider and taller. Using the code below to transform into A4, the page becomes the correct dimensions, but the content is placed in the bottom left corner. Is it possible to decide how the previous content is handled on the crop-up? I would like to have it left side but centered vertically.
pdf.Document pdfDocument = new pdf.Document(path);
PageCollection pageCollection = pdfDocument.Pages;
Page pdfPage = pageCollection[1];
pdfPage.SetPageSize(597.6, 842.4);
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'm using the EvoPDF with which I save HTML to PDF file. The HTML contains long text (can contains lists, tables, etc.). I want to add pages margins, but I don't want to use the pdfConverter.PdfDocumentOptions.{Bottom/Top}Margin - properties, I want to set pages margins into the HTML (something like in Microsoft Word, where I set the page size and margins and text auto move to next page, which has previously set margins). I previewed their Help page, but I couldn't find info about that.
My Convert code is:
EvoPdf.HtmlToPdf.PdfConverter pdfConverter = new EvoPdf.HtmlToPdf.PdfConverter();
pdfConverter.LicenseKey = System.Configuration.ConfigurationManager.AppSettings["EvoHtmlToPdfLicence"];
pdfConverter.PdfDocumentOptions.PdfPageSize = EvoPdf.HtmlToPdf.PdfPageSize.Letter;
pdfConverter.PdfDocumentOptions.PdfPageOrientation = EvoPdf.HtmlToPdf.PdfPageOrientation.Portrait;
byte[] pdf = pdfConverter.GetPdfBytesFromHtmlString(htmlText);
Thank you!
The Margin properties are global thus header, footer, body are affected by this.
If you want to effect the rendered HTML solely, you can use the following settings:
Documentation
HTML Content Destination in PDF. The HTML content destination is given by the X and Y coordinates where to start rendering in first PDF page and by the destination rectangle width and height. All the values are expressed in points. 1 point is 1/72 inches. If you don't set any destination rectangle then by default the converter will start rendering in the top left corner of the first page, will try to use the entire PDF page width for rendering and will auto determine the destination rectangle height such that the entire HTML content is visible. The properties you can set in your code to control the HTML content destination in PDF are X, Y, Width and Height. [...]
HTML Content Top and Bottom Spacing. Using these options you can set a top and a bottom padding for the HTML content. This can be useful for example when you want to introduce a spacing between the PDF page header or footer and the main content. The properties you can set in your code to control the top and bottom spacing are TopSpacing and BottomSpacing. [...]
Code example:
// Category: HTML Content Destination and Spacing Options
// Set HTML content destination in PDF page
if (xLocationTextBox.Text.Length > 0)
htmlToPdfConverter.PdfDocumentOptions.X = float.Parse(xLocationTextBox.Text);
if (yLocationTextBox.Text.Length > 0)
htmlToPdfConverter.PdfDocumentOptions.Y = float.Parse(yLocationTextBox.Text);
if (contentWidthTextBox.Text.Length > 0)
htmlToPdfConverter.PdfDocumentOptions.Width = float.Parse(contentWidthTextBox.Text);
if (contentHeightTextBox.Text.Length > 0)
htmlToPdfConverter.PdfDocumentOptions.Height = float.Parse(contentHeightTextBox.Text);
// Set HTML content top and bottom spacing or leave them not set to have no spacing for the HTML content
htmlToPdfConverter.PdfDocumentOptions.TopSpacing = float.Parse(topSpacingTextBox.Text);
htmlToPdfConverter.PdfDocumentOptions.BottomSpacing = float.Parse(bottomSpacingTextBox.Text);
I'm using iTextSharp to display graph images in a pdf report. Previously we've only had three graphs on one page at a 30% scale. Now I'm adding a 4th image and for some reason it doesn't automatically add it to the next page. Instead it shrinks the image to fit it onto the same page. I'm not very familiar with iTextSharp. Is there a parameter I can set it to print all images at the same size?
If it makes any difference all images are the same size, 720 wide and 380 high.
My code:
Image imageChart = Image.GetInstance(st);
imageChart.ScalePercent(35);
PdfPCell cell = new PdfPCell();
cell.AddElement(imageChart);
Report in its current state (the tiny block highlighted in the bottom left is the 4th image):
I have an existing PDF in which i am trying to add a logo in Header, I have found a good example from
How can I insert an image with iTextSharp in an existing PDF?
It is adding logo in Footer passing 0,0 in image.SetAbsolutePosition(100, 100);
but i want to add logo in Header. If anyone know about it, please suggest.
Are you creating the document from scratch?
If so,
you know the dimensions of the page. It's PageSize.A4 by default, or whatever Rectangle you passed to the Document constructor. You need to adjust the X and Y values depending on the value of that Rectangle. For instance:
image.setAbsolutePosition(rect.Left, rect.Top - image.ScaledHeight);
Where rect is the page size.
As you're adding a header, you want this header to appear on each page, hence you'll use a page event. Take a look at the OnEndPage() method in this example. Make sure you don't add the image bytes as many times as there are pages! Create the image instance outside the onEndPage method, for instance in the constructor of your page event implementation.
If not, you need to get the CropBox of every page:
rect = reader.GetCropBox(page);
If no CropBox was defined, you need to get the MediaBox:
rect = reader.GetPageSize(page);
Where page is a page number (e.g. 1). Based on the value of rect, you can define the position of the image, as shown above.
I hope you understand that your code where you've used x = 0 and y = 0 won't always show the image in the footer. You're making the assumption that the lower-left corner of each page in each PDF has the coordinate (0, 0). That assumption is wrong!
I need to find a way of how to fill whole cover page with a .png picture and put some text in the buttom of a page, where picture wouldnt be.
Right now i got it to stretch by using :
document.DefaultPageSetup.LeftMargin = 0;
document.DefaultPageSetup.TopMargin = 0;
but top margin still leaves som mm of space left (and its not picture which have some white colore in top.)
P.S in future i need to put a picture above the cover page picture. so it actually have to be in 2 layers. Any suggestions?
You don't have to change the page margins to achieve this: images are shapes and shapes can be placed at absolute positions anywhere on the page.
Here's an (untested) code snippet (assuming DIN A4 page size):
var myImage = section.Headers.FirstPage.AddImage("ImageLocation");
myImage.Height = "29.7cm";
myImage.Width = "21cm";
myImage.RelativeVertical = RelativeVertical.Page;
myImage.RelativeHorizontal = RelativeHorizontal.Page;
myImage.WrapFormat.Style = WrapStyle.Through;
The trick is to use "WrapStyle.Through" and make positions relative to the page.
This should also solve your "P. S." question.