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);
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 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);
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 want to create a table in a PDF document which would have variable width of columns depending on the width of its contents.
I am using PDFSharp, MigraDoc
Maybe getting the paragraph width from the individual cells will help. Is there a way to get text width depending on the font / styles?
Any clues?
Thanks.
Using PDFsharp functions, you can get the width of any text.
MigraDoc is not limited to PDF, it creates documents for PDF, RTF, print. No chance to determine the exact width (unless you restrict yourself to PDF and use PDFsharp to get the width).
MigraDoc will break text in columns to the next line when needed.
Create a dummy PdfDocument, create a page, get an XGraphics object (gfx) for that page, then use gfx.MeasureString() to find the width.
I am trying to create a pdf document in c# using iTextSharp 5.0.6. I want to add header and footer to every page in OnStartPage and OnEndPage events respectively.
In case of footer there is a problem that the footer is created right where the page ends whereas I would like to be at the bottom of page.
Is there a way in iTextSharp to specify page height so that footer is always created at the bottom.
Thanks!
The page's height is always defined:
document.PageSize.Height // document.getPageSize().getHeight() in Java
Keep in mind that in PDF 0,0 is the lower left corner, and coordinates increase as you go right and UP.
Within a PdfPageEvent you need to use absolute coordinates. It sounds like you're either getting the current Y from the document, or Just Drawing Stuff at the current location. Don't do that.
Also, if you want to use the same exact footer on every page, you can draw everything into a PdfTemplate, then draw that template into the various pages on which you want it.
PdfTemplate footerTmpl = writer.getDirectContent().createTemplate( 0, 0, pageWidth, footerHeight );
footerTmpl.setFontAndSize( someFont, someSize );
footerTmpl.setTextMatrix( x, y );
footer.showText("blah");
// etc
Then in your PdfPageEvent, you can just add footerTempl at the bottom of your page:
writer.getDirectContent().addTemplateSimple( footerTmpl, 0, 0 );
Even if most of your footer is the same, you can use this technique to save memory, execution time, and file size.
Furthermore, if you don't want to mess with PdfContentByte drawing commands directly, you can avoid them to some extent via ColumnText. There are several SO questions tagged with iText or iTextSharp dealing with that class. Poke around, you'll find them.