6

I have an HTML string, i need to convert it to pdf, but pdf that i need must have specific size and margin. I did as the example show, now i have pdf with width and height that i set, BUT i can`t change or delete the margin, so pls help me.

 using (FileStream fs = new FileStream(somePDFFile, FileMode.OpenOrCreate, FileAccess.Write))
            {

                iText.Kernel.Pdf.PdfWriter pdfWriter = new iText.Kernel.Pdf.PdfWriter(fs);

                iText.Kernel.Pdf.PdfDocument pdfDoc = new iText.Kernel.Pdf.PdfDocument(pdfWriter);

                var v = pdfDoc.GetDefaultPageSize().ApplyMargins<iText.Kernel.Geom.Rectangle>(1, 1, 1, 1, true);
                pdfDoc.GetDefaultPageSize().SetWidth(250f);
                pdfDoc.GetDefaultPageSize().SetHeight(200f);
                pdfDoc.GetCatalog().SetLang(new iText.Kernel.Pdf.PdfString("en-US"));
                //Set the document to be tagged
                pdfDoc.SetTagged();



                iText.Html2pdf.ConverterProperties props = new iText.Html2pdf.ConverterProperties();

                iText.Html2pdf.HtmlConverter.ConvertToPdf(htmlString, pdfDoc, props);

                pdfDoc.Close();



            }
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
Petr Gavrilov
  • 71
  • 1
  • 1
  • 4

1 Answers1

17

I searched for an answer, but I could only find this approach:

public void createPdf(String src, String dest) throws IOException {
    ConverterProperties properties = new ConverterProperties();
    properties.setBaseUri(new File(src).getParent());
    List<IElement> elements =
            HtmlConverter.convertToElements(new FileInputStream(src), properties);
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    pdf.setTagged();
    Document document = new Document(pdf);
    document.setMargins(100, 50, 50, 100);
    for (IElement element : elements) {
        document.add((IBlockElement)element);
    }
    document.close();
}

In other words: I convert the HTML to a list of elements, and I then add those elements to a Document for which I define a margin.

My preferred solution would have been to define the margin at the level of the <body> tag as done in How to margin the body of the page (html)? Unfortunately, I noticed that this isn't supported yet (and I made a ticket for the iText development team to fix this).

I also tried the convertToDocument() method, but I wasn't able to set immediateFlush to false. I also asked the team to look into this.

Maybe there's also a property that could be introduced, although I'm not all that sure if this should be a ConverterProperties property, a PdfDocument property, or a PdfWriter property.

Update:

You could use the @page rule in CSS to define the margins. For instance:

<style>
    @page {
        margin-top: 200pt;
    }
</style>

This creates a PDF with a top margin of 200pt.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Works fine. thanks, i will ask another thing, because maybe i chose complicate way in the begining. As a initial data i have not only HTML, i use very old util that can convert HTML to PDF, but without any settings so as output i get PDF as A4 format with standart margin. But as i told, i need to create PDF with custom width and height and margin. So maybe there is a way to modifay existed PDF pocument from A4 to custom size and custom margin(but i have not found the way) or create a new Document with setting(its easy) but i have not found how to add content from existed PDF to new document. – Petr Gavrilov Nov 24 '17 at 13:35
  • I don't know if I understand your question. [1.] You can't *reflow* content from an existing PDF. That's not an iText thing; that's inherent to PDF. [2.] You can change the page size of an existing PDF, but the content remains unaltered. [3.] You can import complete pages of an existing PDF into a new PDF. I'm not sure if that answers all your additional questions. Feel free to post a new question in which you elaborate which of the three things above applies to your question. – Bruno Lowagie Nov 24 '17 at 13:45
  • Sorry for my english, i am not native. So as i understand, the way when i have PDF as A4 and i resize it for example to 300*200, the content will stay as it was, and in this new rectangle i will see part of content from original, content will not resize itself for new proportion. And another, so if i have PDF as A4 with 2 lines and qr-code only for example, can i take this 2 lines and qr-code, and put in in new PDF with my custom size and all the settings, because i need PDF as small rectangle, only for this content without many free spaces. If yes, pls show me the way to the light)))Thanks. – Petr Gavrilov Nov 24 '17 at 14:43
  • The first part of your question: yes, you have understood well. The content won't change; you'll "clip" the existing content if you make the page size smaller. As for the second part of your question: it's not impossible, but it's difficult. @mkl has answered a similar question a while ago, but I can't find that answer. Maybe you can post it as a new question so that mkl can answer. – Bruno Lowagie Nov 24 '17 at 15:32
  • In any way, thank you very much. I`ve found the solution and as i understand, its a best way if you need to create a PDF with special settings from HTML. I will ask the question about copy from one to another in a new post. – Petr Gavrilov Nov 24 '17 at 20:22
  • Thank you @BrunoLowagie for your answer. – shaishav shukla Aug 05 '20 at 07:47