Programming/c# & winform2020. 10. 22. 16:51

HtmlConverter.ConvertToPdf()

는 하나의 HTML을 pdf로 변환하고 close 하기 때문에

merger를 이용해서 합쳐야 하는 듯 하고

 

HtmlConverter.ConvertToDocument()

는 pdf로 변환하지만 ConvertToPdf()와는 다르게 div()를 정상적으로 처리하지 못한다.

 

[링크 : https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-converting-html-to-pdf-with-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/how-to-parse-multiple-html-files-into-one-pdf] java

 

private void button2_Click(object sender, EventArgs e)
        {
            String DEST = "hello_world.pdf";

            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            PdfWriter writer = new PdfWriter(DEST);
            PdfDocument pdf = new PdfDocument(writer);
            Document document = new Document(pdf, PageSize.A4.Rotate());
            document.SetMargins(20, 20, 20, 20);
            PdfMerger pdfMerger = new PdfMerger(pdf);

            document.Add(new Paragraph("Hello World!"));

            String[] SRC = {
            @"d:\test1.html",
            @"d:\test2.html",
            @"d:\test3.html",
            @"d:\test4.html" };

            foreach (var html in SRC)
            {
                MemoryStream baos = new MemoryStream();
                PdfDocument temp = new PdfDocument(new PdfWriter(baos));
                temp.SetDefaultPageSize(PageSize.A4.Rotate());
                HtmlConverter.ConvertToPdf(System.IO.File.ReadAllText(html), temp, new ConverterProperties());
                ReaderProperties rp = new ReaderProperties();
                baos = new MemoryStream(baos.ToArray());
                temp = new PdfDocument(new PdfReader(baos, rp));
                pdfMerger.Merge(temp, 1, temp.GetNumberOfPages());
                temp.Close();
            }
            pdfMerger.Close();            
        }

[링크 : https://stackoverflow.com/questions/57415902/generate-one-pdf-document-with-multiple-pages-converting-from-html-using-itext-7] .net

 

pdf 특성상 붙이는 파일의 용지 방향을 지정해 주어야 한다.

[링크 : https://stackoverflow.com/questions/54347293/how-to-set-orientation-to-landscape-in-itext-7]

'Programming > c# & winform' 카테고리의 다른 글

markdig custom markdown  (0) 2020.10.22
pdfsharp , migradoc  (0) 2020.10.22
c# pdf itextsharp -> itext7  (0) 2020.10.22
c# print 하기  (0) 2020.10.19
c# printer 사용하기 - printer enumeration  (0) 2020.10.19
Posted by 구차니