This is an archived forum post. The information may be outdated. Contact us if you have any questions.
public class PDFCreator { public PDFCreator() { } public static void ConvertHtmlToPDF(string uri) { System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response; try { // create an API client instance - pdfcrowd.Client client = new pdfcrowd.Client("UNAME", "API_CODE"); client.setHorizontalMargin(0); client.setVerticalMargin(0); client.setPageWidth("8.5in"); client.setPageHeight("11.0in"); int partNum = 1; FileStream StreamPart1 = new FileStream("../documents/catalog/part" + partNum.ToString() + ".pdf", FileMode.CreateNew); client.convertURI(uri + "?start_page=1", StreamPart1); StreamPart1.Close(); partNum++; FileStream StreamPart2 = new FileStream("../documents/catalog/part" + partNum.ToString() + ".pdf", FileMode.CreateNew); client.convertURI(uri + "?start_page=27", StreamPart2); StreamPart2.Close(); partNum++; FileStream StreamPart3 = new FileStream(../documents/catalog/part" + partNum.ToString() + ".pdf", FileMode.CreateNew); client.convertURI(uri + "?start_page=53", StreamPart3); StreamPart3.Close(); partNum++; FileStream StreamPart4 = new FileStream("../documents/catalog/part" + partNum.ToString() + ".pdf", FileMode.CreateNew); client.convertURI(uri + "?start_page=79", StreamPart4); StreamPart4.Close(); partNum++; FileStream StreamPart5 = new FileStream("../documents/catalog/part" + partNum.ToString() + ".pdf", FileMode.CreateNew); client.convertURI(uri + "?start_page=105", StreamPart5); StreamPart5.Close(); } catch (pdfcrowd.Error why) { Response.Write(why.ToString()); } } }
public class AptaCatalog { public AptaCatalog() { } public static void CreateCatalogFromParts() { string destination_file = ".../documents/catalog/catalog.pdf"; string[] source_files = new string[5] { ".../documents/catalog/part1.pdf", ".../documents/catalog/part2.pdf", ".../documents/catalog/part3.pdf", ".../documents/catalog/part4.pdf", ".../documents/catalog/part5.pdf"}; MergeFiles(destination_file, source_files); AppendPageNumbers(); } public static void MergeFiles(string destinationFile, string[] sourceFiles) { try { int f = 0; String outFile = destinationFile; Document document = null; PdfCopy writer = null; while (f < sourceFiles.Length) { // Create a reader for a certain document PdfReader reader = new PdfReader(sourceFiles[f]); // Retrieve the total number of pages int n = reader.NumberOfPages; //Trace.WriteLine("There are " + n + " pages in " + sourceFiles[f]); if (f == 0) { // Step 1: Creation of a document-object document = new Document(reader.GetPageSizeWithRotation(1)); // Step 2: Create a writer that listens to the document writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create)); // Step 3: Open the document document.Open(); } // Step 4: Add content PdfImportedPage page; for (int i = 0; i < n; ) { ++i; page = writer.GetImportedPage(reader, i); writer.AddPage(page); } PRAcroForm form = reader.AcroForm; if (form != null) writer.CopyAcroForm(reader); f++; } // Step 5: Close the document document.Close(); } catch (Exception) { //handle exception } } public static void AppendPageNumbers() { PdfReader reader1 = new PdfReader(".../documents/catalog/catalog.pdf"); PdfStamper stamper = new PdfStamper(reader1, new FileStream(".../documents/catalog/Aptagen_AptamerCatalog_" + DateTime.Now.Year.ToString() + ".pdf", FileMode.Create)); BaseFont font = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); // Helvetica, WinAnsiEncoding for (int i = 0; i < reader1.NumberOfPages; ++i) { if (i != 0) { PdfContentByte overContent = stamper.GetOverContent(i + 1); overContent.SaveState(); overContent.BeginText(); overContent.SetFontAndSize(font, 10.0f); overContent.SetTextMatrix(270, 15); overContent.ShowText("Page " + (i + 1) + " of " + reader1.NumberOfPages.ToString()); overContent.EndText(); overContent.RestoreState(); } } stamper.Close(); File.Delete(".../documents/catalog/catalog.pdf"); } }