Multiple page HTML not rendering properly in HtmlRenderer.PdfSharp - c#

I have created a sample application in which i have used nuget package HtmlRenderer.PdfSharp
I used the below code in my console application to generate pdf from html:
string input = File.ReadAllText("..//..//HTML//dfdf.html");
PdfDocument document = new PdfDocument();
document.Info.Title = "Test";
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
//XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
// Create a font
TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerateConfig pdfconfg = new TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerateConfig();
pdfconfg.PageSize = PdfSharp.PageSize.A4;
document = PdfGenerator.GeneratePdf(input, pdfconfg);
document.Save("..//..//PDF//dfdf.pdf");
But when the text of html page is very long then it is not rending the content. Only blank pages are generating. Please help. I want to use this library as it provide desired output.

Related

iTextSharp Overlay two pdf files

I have 2 pdf documents. One with the actual content pdf, the second one is a letterhead pdf. I'm trying to overlay one on the other one but the issue is once overlapped content is over letterhead as well. Here is the code that I have recently. My question is how can I put the content between header and footer? Thanks in advance.
string originalPdfPath = #"C:\Users\BiberEmr\Downloads\testPdf\ThreePageContent.pdf";
string letterHeadPath = #"C:\Users\BiberEmr\Downloads\testPdf\letterhead_lorenzo.pdf";
// Opening reader on content PDF and creating stamper
PdfReader contentReader = new PdfReader(originalPdfPath);
PdfStamper stamper = new PdfStamper(contentReader, new FileStream(#"C:\Users\BiberEmr\Downloads\testPdf\result2.pdf", FileMode.Create));
// Opening template pdf
PdfReader templateReader = new PdfReader(letterHeadPath);
PdfImportedPage templatePage = stamper.GetImportedPage(templateReader, 1);
// Looping through pages
for (int i = 1; i <= contentReader.NumberOfPages; i++)
{
// Retrieve content page where to apply template
PdfContentByte contentPage = stamper.GetUnderContent(i);
// Apply template to PDF content
contentPage.AddTemplate(templatePage, 0, 50);
}
stamper.Close();
templateReader.Close();
contentReader.Close();
Here is the current output
Page 1
Page2

save as iText.Layout.Element.Image in folder

i want to save pdf page as image but no success. i am able to create iText.Layout.Element.Image from pdf with itext7 but stuck here
using var pdfreader = new PdfReader("../../../documents/valid.pdf");
PdfDocument origPdf = new PdfDocument(pdfreader);
PdfPage origPage = origPdf.GetPage(1);
using var stream = new MemoryStream();
using var pdfwriter = new PdfWriter(stream);
PdfDocument pdf = new PdfDocument(pdfwriter);
Document document = new Document(pdf);
PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
Image image = new Image(pageCopy);
// want to save this image
The iText 7 Image class is (according to JavaDocs) a layout element that represents an image for inclusion in the document model. It essentially can wrap arbitrary contents to be added to the contents of some page (or form XObject, ...) in an image like manner. It is not, however, an arbitrary-content-to-bitmap converter.
If you want to render a page as a bitmap using iText 7 components, consider using the iText 7 Core add-on pdfRender.

iText7 - Add text to the bottom of existing pdf

I need to add some text content to the bottom of an existing .pdf document with iText. I have a working method, but the new content is displayed in the top-left corner, overlapping the existing content:
public PdfDocument GetDocumentWithAppendedContent(string path, string content)
{
var stream = new MemoryStream();
var writer = new PdfWriter(stream);
writer.SetCloseStream(false); // so I can reuse stream to create a readonly document later
var pdfResult = new PdfDocument(new PdfReader(path), writer);
var document = new Document(pdfResult);
var div = new Div().SetMargin(0).SetPadding(0).SetKeepTogether(true);
div.Add(new Paragraph(content));
document.Add(div);
document.Close();
pdfResult.Close();
stream.Position = 0;
return new PdfDocument(new PdfReader(stream)); // return a readonly version
}
How to "move" the content at the bottom of the last page, instead of the beginning of the first? I am new to iText and, surprisingly, can't find a solution online.
You can do it using the code like this.
PdfPage page = doc.GetPage(doc.GetNumberOfPages());
//Create canvas fro the last page
Canvas canvas = new Canvas(page, page.GetPageSize());
//Set fixed position to put the div at the left bottom corner of the canvas
div.SetFixedPosition(0, 0, page.getPageSize().getWidth());
canvas.Add(p);
doc.Close();

HiQPdf throws "Cannot layout the image" when rendering image

I'm working on rendering an image on a pdf document. The rendering is done in C# method like this:
public void PrintPdf(System.Drawing.Image image, string htmlCode, string url)
{
PdfDocument Document = new PdfDocument();
Document.Margins = new PdfDocumentMargins(20, 20, 20, 20);
Document.ImagesCompression = 0;
Document.Compress = false;
Document.SerialNumber = "our-serial-number";
PdfPage page = Document.AddPage(PdfPageSize.A4, Document.Margins, PdfPageOrientation.Portrait);
PdfHtml overlayHtml = new PdfHtml(0, 0, htmlCode, url);
overlayHtml.BrowserWidth = 740;
overlayHtml.MediaType = "print";
PdfLayoutInfo layoutInfo = page.Layout(overlayHtml);
PdfImage pdfImage = new PdfImage(0, (float)115.5, image);
layoutInfo = page.Layout(pdfImage); // <-- this row causes exception from the HiQPdf library
}
The last row throws the following exception from the HiQPdf library. Any thoughts on why?
[HiQPdfException: Cannot layout the image]
HiQPdf.PdfImage.LayoutObject(PdfCanvas objectsContainer) +7243
HiQPdf.PdfPage.Layout(PdfObject pdfObject) +86
This error started happening after we updated HiQPdf version from 5.4.0.0 to 10.17.0.0
The parameters seem ok, there is normal html in htmlCode and I don't see anything wrong with the image either. The image itself can be downloaded as png well as pdf, which works fine.

Getting Paper Size of a PDF

I am using PDFsharp to read / write PDF in our application. I am trying to get the paper size of a page to show in metadata window. I am using following code to do the same:
// read PDF document into memory
      var pdfDocument = PdfReader.Open(pdfPath);
      // if PDF document has more than one page, then try to get the page size from first page
      if (pdfDocument.Pages.Count > 0)
      {
        var pageSize = pdfDocument.Pages[0].Size.ToString();
      } // if
But every time it returns 'Undefined', I even tried to create A4 page document for MS Word. But still it returns 'Undefined'.
I also created a PDF from HTML but then also page size comes as undefined.
static void Main(string[] args)
{
// create PDF config to generate PDF
var config = new PdfGenerateConfig()
{
PageSize = PageSize.Letter,
PageOrientation = PageOrientation.Landscape
};
// load the HTML file
var html = System.IO.File.ReadAllText(#"C:\Users\mohit\Desktop\Temp\Ekta\Test.html");
// generate the PDF
var pdf = PdfGenerator.GeneratePdf(html,
PageSize.Letter);
// get the paper size of first page
var size = pdf.Pages[0].Size;
// save the pdf document
pdf.Save(#"C:\Users\mohit\Desktop\Temp\Ekta\A13.pdf");
Console.ReadKey();
}
The properties you have to use are Width and Height. Convert them to millimeter or inch if you do not want to show the size in points.
The property Size is a convenient way to set standard page sizes for new pages as you can specify A4 or Letter. It will not be set for imported PDF files.

Categories

Resources