using System;
using System.Collections.Generic;
using System.IO;
using iText.Commons.Utils;
using iText.Html2pdf;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Licensing.Base;
namespace iText.Samples.Htmlsamples.Chapter07
{
///
/// Can we parse different HTML files and combine them into one PDF?
/// Yes, this can be done in different ways. This example shows how
/// to convert HTML to iText elements, and how to add the elements
/// of the different HTML files to a single PDF document.
///
public class C07E02_CombineHtml2
{
///
/// The Base URI of the HTML page.
///
public static readonly String BASEURI = "../../../resources/htmlsamples/html/";
///
/// An array containing the paths to different HTML files.
///
public static readonly String[] SRC =
{
String.Format("{0}invitation.html", BASEURI),
String.Format("{0}sxsw.html", BASEURI),
String.Format("{0}movies.html", BASEURI)
};
///
/// The path to the resulting PDF file.
///
public static readonly String DEST = "results/htmlsamples/ch07/bundle2.pdf";
///
/// The main method of this example.
///
/// no arguments are needed to run this example.
public static void Main(String[] args)
{
using (Stream license = FileUtil.GetInputStreamForFile(
Environment.GetEnvironmentVariable("ITEXT7_LICENSEKEY") + "/itextkey-html2pdf_typography.json"))
{
LicenseKey.LoadLicenseFile(license);
}
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new C07E02_CombineHtml2().CreatePdf(BASEURI, SRC, DEST);
}
///
/// Creates the PDF file.
///
/// the base URI
/// an array with the paths to different source HTML files
/// the path to the resulting PDF
public void CreatePdf(String baseUri, String[] src, String dest)
{
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(baseUri);
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
foreach (String html in src)
{
IList elements = HtmlConverter.ConvertToElements(
new FileStream(html, FileMode.Open, FileAccess.Read), properties);
foreach (IElement element in elements)
{
document.Add((IBlockElement) element);
}
}
document.Close();
}
}
}