/** * Use this method to get an instance of the <CODE>PdfWriter</CODE>. * @param document The <CODE>Document</CODE> that has to be written * @param os The <CODE>Stream</CODE> the writer has to write to. * @param conformanceLevel PDF/A conformance level of a new PDF document * @return a new <CODE>PdfWriter</CODE> * @throws DocumentException on error */ public static PdfAWriter GetInstance(Document document, Stream os, PdfAConformanceLevel conformanceLevel) { PdfDocument pdf = new PdfDocument(); document.AddDocListener(pdf); PdfAWriter writer = new PdfAWriter(pdf, os, conformanceLevel); pdf.AddWriter(writer); return writer; }
internal PdfCopyFieldsImp(Stream os, char pdfVersion) : base(new PdfDocument(), os) { pdf.AddWriter(this); if (pdfVersion != 0) base.PdfVersion = pdfVersion; nd = new Document(); nd.AddDocListener(pdf); }
/** * Use this method to get an instance of the <CODE>PdfWriter</CODE>. * @param document The <CODE>Document</CODE> that has to be written * @param os The <CODE>Stream</CODE> the writer has to write to. * @param conformanceLevel PDF/A conformance level of a new PDF document * @return a new <CODE>PdfWriter</CODE> * @throws DocumentException on error */ public static PdfAWriter GetInstance(Document document, Stream os, PdfAConformanceLevel conformanceLevel) { PdfDocument pdf = new PdfDocument(); document.AddDocListener(pdf); PdfAWriter writer = new PdfAWriter(pdf, os, conformanceLevel); pdf.AddWriter(writer); return(writer); }
internal PdfCopyFieldsImp(Stream os, char pdfVersion) : base(new PdfDocument(), os) { pdf.AddWriter(this); if (pdfVersion != 0) { base.PdfVersion = pdfVersion; } nd = new Document(); nd.AddDocListener(pdf); }
private static void SetPdfAConformance(PdfReader reader, Document doc, MemoryStream ms) { // Create PdfAWriter with PdfAConformanceLevel.PDF_A_3B option if you // want to get a PDF/A-3b compliant document. PdfAWriter writer = PdfAWriter.GetInstance(doc, ms, _pdfaConformanceLevel); // Create XMP metadata. It's a PDF/A requirement. writer.CreateXmpMetadata(); doc.Open(); // Set output intent. PDF/A requirement. ICC_Profile icc = ICC_Profile .GetInstance(new FileStream(@"resources/sRGB Color Space Profile.icm", FileMode.Open)); writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc); // Creating PDF/A-3 compliant attachment. PdfDictionary parameters = new PdfDictionary(); parameters.Put(PdfName.MODDATE, new PdfDate()); PdfFileSpecification fileSpec = PdfFileSpecification.FileEmbedded( writer, _inputFilePath, "test.pdf", null, "application/pdf", parameters, 0); fileSpec.Put(new PdfName("AFRelationship"), new PdfName("Data")); writer.AddFileAttachment("test.pdf", fileSpec); PdfArray array = new PdfArray { fileSpec.Reference }; writer.ExtraCatalog.Put(new PdfName("AF"), array); doc.AddDocListener(writer); for (int i = 1; i <= reader.NumberOfPages; i++) { doc.SetPageSize(reader.GetPageSize(i)); doc.NewPage(); PdfContentByte cb = writer.DirectContent; PdfImportedPage page = writer.GetImportedPage(reader, i); int rotation = reader.GetPageRotation(i); if (rotation == 90 || rotation == 270) { cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height); } else { cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0); } } }
/// <summary> /// constructor /// </summary> /// <summary> /// Constructs a HtmlWriter . /// </summary> /// <param name="doc">The Document that has to be written as HTML</param> /// <param name="os">The Stream the writer has to write to.</param> protected HtmlWriter(Document doc, Stream os) : base(doc, os) { Document.AddDocListener(this); PageN = Document.PageNumber; os.WriteByte(LT); byte[] tmp = GetIsoBytes(HtmlTags.HTML); os.Write(tmp, 0, tmp.Length); os.WriteByte(GT); os.WriteByte(NEWLINE); os.WriteByte(TAB); os.WriteByte(LT); tmp = GetIsoBytes(HtmlTags.HEAD); os.Write(tmp, 0, tmp.Length); os.WriteByte(GT); }
public void GenerateMBLPDF() { // Source 파일 불러올 파일경로 string SOURCE_FILE_NAME = "MBL_SOURCE.pdf"; string SOURCE_PATH_FILENAME = sourcePdfPath + SOURCE_FILE_NAME; // 저장경로 생성 Util.CreateFolder(savePdfPath); string _pdfFile = mbl.mblNo.Trim() + ".pdf"; string _PDF = savePdfPath + _pdfFile; try { // MASTER BL MemoryStream pdfMBL = setFormMBL(SOURCE_PATH_FILENAME); PdfReader reader1 = new PdfReader(pdfMBL.GetBuffer()); doc = new Document(PageSize.LETTER, 0, 0, 0, 0); Document.Compress = true; writer = PdfWriter.GetInstance(doc, new FileStream(_PDF, FileMode.Create)); doc.AddDocListener(writer); doc.Open(); //PdfContentByte cb = writer.DirectContent; cb = writer.DirectContent; PdfImportedPage page; #region MASTER BL doc.NewPage(); page = writer.GetImportedPage(reader1, 1); cb.AddTemplate(page, 0, 0); #endregion // Set additional information doc.AddCreationDate(); doc.AddSubject(""); doc.AddCreator(""); doc.AddAuthor(""); doc.Close(); reader1.Close(); } catch (Exception ex) { throw ex; } }
public static void TestCreatePDF1() { try { Document document = new Document(); FileStream objFileStream = new System.IO.FileStream("Output.pdf", System.IO.FileMode.Create); PdfWriter writer = PdfWriter.GetInstance(document, objFileStream); document.Open(); document.AddDocListener(writer); PdfReader reader = new PdfReader("Input.pdf"); Document inputDocument = null; PdfCopy pdfCopyProvider = null; inputDocument = new Document(reader.GetPageSizeWithRotation(1)); pdfCopyProvider = new PdfCopy(inputDocument, objFileStream); inputDocument.Open(); PdfImportedPage importedPage = null; importedPage = pdfCopyProvider.GetImportedPage(reader, 1); pdfCopyProvider.Add(new Chunk("Chapter 1").SetLocalDestination("1")); pdfCopyProvider.AddPage(importedPage); // Code 2 PdfContentByte cb = writer.DirectContent; PdfOutline root = cb.RootOutline; // Code 3 PdfOutline oline1 = new PdfOutline(root, PdfAction.GotoLocalPage(1, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer), "Chapter 1"); reader.Close(); //writer.Close(); inputDocument.Close(); //objFileStream.Close(); //document.Close(); } catch (Exception ex) { } }
static void ConvertTextToPDF() { StreamReader reader = new StreamReader("../../test.txt"); Document doc = new Document(); doc.AddDocListener(new DocListener()); PdfWriter.GetInstance(doc, new FileStream("../../test.pdf", FileMode.Create)); doc.Open(); int counter = 0; while (reader.Peek() >= 0) { string content = reader.ReadLine(); content = content.Trim(); if (!string.IsNullOrWhiteSpace(content)) { var matches = Regex.Matches(content, @"\b[\w']*\b"); foreach (Match m in matches) { if (!string.IsNullOrWhiteSpace(m.Value) && !int.TryParse(m.Value, out _)) { doc.Add(new Paragraph(m.Value)); counter++; } if (counter == 30) { counter = 0; doc.NewPage(); } } } } doc.Close(); }
/** * Use this method to get an instance of the <CODE>PdfWriter</CODE>. * * @return a new <CODE>PdfWriter</CODE> * @param document The <CODE>Document</CODE> that has to be written * @param os The <CODE>Stream</CODE> the writer has to write to. * @param listener A <CODE>DocListener</CODE> to pass to the PdfDocument. * @throws DocumentException on error */ public static PdfWriter GetInstance(Document document, Stream os, IDocListener listener) { PdfDocument pdf = new PdfDocument(); pdf.AddDocListener(listener); document.AddDocListener(pdf); PdfWriter writer = new PdfWriter(pdf, os); pdf.AddWriter(writer); return writer; }
/// <summary> /// Constructor /// </summary> /// <param name="document"></param> /// <param name="os">outputstream</param> public PdfCopy(Document document, Stream os) : base(new PdfDocument(), os) { document.AddDocListener(Pdf); Pdf.AddWriter(this); IndirectMap = new Hashtable(); }
/** * Constructor * @param document * @param os outputstream */ public PdfCopy(Document document, Stream os) : base(new PdfDocument(), os) { document.AddDocListener(pdf); pdf.AddWriter(this); indirectMap = new Dictionary <PdfReader, Dictionary <RefKey, IndirectReferences> >(); }
/** * Constructor * @param document * @param os outputstream */ public PdfCopy(Document document, Stream os) : base(new PdfDocument(), os) { document.AddDocListener(pdf); pdf.AddWriter(this); indirectMap = new Dictionary<PdfReader,Dictionary<RefKey,IndirectReferences>>(); parentObjects = new Dictionary<PdfObject, PdfObject>(); disableIndirects = new HashSet2<PdfObject>(); indirectObjects = new Dictionary<RefKey, PdfIndirectObject>(); savedObjects = new List<PdfIndirectObject>(); importedPages = new List<ImportedPage>(); }
public void DeletePages(string pageRange, string SourcePdfPath, string OutputPdfPath, string Password = "") { List <int> pagesToDelete = new List <int>(); // check for non-consecutive ranges if (pageRange.IndexOf(",") != -1) { string[] tmpHold = pageRange.Split(','); foreach (string nonconseq in tmpHold) { // check for ranges if (nonconseq.IndexOf("-") != -1) { string[] rangeHold = nonconseq.Split('-'); for (int i = Convert.ToInt32(rangeHold[0]); i <= Convert.ToInt32(rangeHold[1]); i++) { pagesToDelete.Add(i); } } else { pagesToDelete.Add(Convert.ToInt32(nonconseq)); } } } else { // check for ranges if (pageRange.IndexOf("-") != -1) { string[] rangeHold = pageRange.Split('-'); for (int i = Convert.ToInt32(rangeHold[0]); i <= Convert.ToInt32(rangeHold[1]); i++) { pagesToDelete.Add(i); } } else { pagesToDelete.Add(Convert.ToInt32(pageRange)); } } Document doc = new Document(); PdfReader reader = new PdfReader(SourcePdfPath, new System.Text.ASCIIEncoding().GetBytes(Password)); using (MemoryStream memoryStream = new MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream); doc.Open(); doc.AddDocListener(writer); for (int p = 1; p <= reader.NumberOfPages; p++) { if (pagesToDelete.FindIndex(s => s == p) != -1) { continue; } doc.SetPageSize(reader.GetPageSize(p)); //doc.SetPageSize(PageSize.A4); doc.NewPage(); PdfContentByte cb = writer.DirectContent; PdfImportedPage pageImport = writer.GetImportedPage(reader, p); //int rot = reader.GetPageRotation(p); //if (rot == 90 || rot == 270) // cb.AddTemplate(pageImport, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height); //else // cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0); cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0); } reader.Close(); doc.Close(); File.WriteAllBytes(OutputPdfPath, memoryStream.ToArray()); } }
/** * Constructs a new RtfWriter that listens to the specified Document and * writes its output to the Stream. * * @param doc The Document that this RtfWriter listens to * @param os The Stream to write to */ protected RtfWriter2(Document doc, Stream os) : base(doc, os) { doc.AddDocListener(this); rtfDoc = new RtfDocument(); }
public static bool SaveTreeStructureToPDF(Dictionary <int, Dictionary <string, string> > bListFilePages, string strOutputPath) { bool bReturn = false; string strPreviousFile = "previous"; string strCurrentFile = "Current"; string OutPutfile = strOutputPath; PdfReader inputReader = null; PdfOutline outline = null; int iPageCount = 1; try { Document outputDoc = new Document(); using (MemoryStream ms = new MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(outputDoc, ms); outputDoc.Open(); outputDoc.AddDocListener(writer); for (int iIndex = 0; iIndex < bListFilePages.Count; iIndex++) { Dictionary <string, string> innerDictionary = bListFilePages.ElementAt(iIndex).Value;//.Value.ToString(); string strBookMarkName = innerDictionary.ElementAt(0).Key.ToString(); string tempPath = innerDictionary.ElementAt(0).Value.ToString(); if (tempPath.Length > 0) { string strFilePath; string strPageNumber; PdfImportedPage page = null; int iPageNumber = 1; int i = tempPath.IndexOf("#@$"); if (i != -1) { strFilePath = tempPath.Substring(0, i - 1); strPageNumber = tempPath.Substring(i + 3); strCurrentFile = strFilePath; if (strCurrentFile != strPreviousFile) { if (inputReader != null) { inputReader.Close(); inputReader = null; } inputReader = new PdfReader(strCurrentFile); } if (IsNumber(strPageNumber)) { if (strFilePath.Length > 0 && strPageNumber.Length > 0) { iPageNumber = Convert.ToInt16(strPageNumber); outputDoc.SetPageSize(inputReader.GetPageSize(iPageNumber)); PdfContentByte cb = writer.DirectContent; page = writer.GetImportedPage(inputReader, iPageNumber); ///creat bookmark here and add in template PdfOutline root = cb.RootOutline; ArrayList outlineCollection = root.Kids; outline = CheckIfOutlineISPresent(strBookMarkName, outlineCollection); if (outline == null) { outline = new PdfOutline(root, PdfAction.GotoLocalPage(iPageCount, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer), strBookMarkName); } else { outline = new PdfOutline(outline, PdfAction.GotoLocalPage(iPageCount, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer), strBookMarkName); } outputDoc.NewPage(); Rectangle psize = inputReader.GetPageSizeWithRotation(iPageNumber); switch (psize.Rotation) { case 0: cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); break; case 90: cb.AddTemplate(page, 0, -1f, 1f, 0, 0, psize.Height); break; case 180: cb.AddTemplate(page, -1f, 0, 0, -1f, 0, 0); break; case 270: cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, psize.Width, 0); break; default: break; } iPageCount++; } } strPreviousFile = strCurrentFile; } else { if (File.Exists(tempPath)) { strCurrentFile = tempPath; } else { strCurrentFile = GetFirstFileFromTree(iIndex, bListFilePages); } if (!File.Exists(strCurrentFile)) { continue; } iPageNumber = 1; PdfContentByte cb = writer.DirectContent; ///creat bookmark here and add in template PdfOutline root = cb.RootOutline; ArrayList outlineCollection = root.Kids; outline = CheckIfOutlineISPresent(strBookMarkName, outlineCollection); if (outline == null) { outline = new PdfOutline(root, PdfAction.GotoLocalPage(iPageCount, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer), strBookMarkName); } else { outline = new PdfOutline(outline, PdfAction.GotoLocalPage(iPageCount, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer), strBookMarkName); } } } } if (inputReader != null) { inputReader.Close(); inputReader = null; } outputDoc.Close(); using (FileStream fs = new FileStream(OutPutfile, FileMode.Create)) { // this is the part stumping me; I need to use a PdfStamper to write // out some values to fields on the form AFTER the pages are removed. // This works, but there doesn't seem to be a form on the copied page... PdfStamper stamper = new PdfStamper(new PdfReader(ms.ToArray()), fs); // write out fields here... stamper.FormFlattening = true; stamper.SetFullCompression(); stamper.Close(); } bReturn = true; } } catch (Exception ex) { bReturn = false; } return(bReturn); }