private static Annot setStamperImage(StamperImage stp) { var page = _currentDoc.GetPage(stp.Page()); using (pdftron.PDF.Stamper s = new pdftron.PDF.Stamper(pdftron.PDF.Stamper.SizeType.e_relative_scale, .5, .5)) { s.SetAsAnnotation(true); var rect = AnnotationsMannager.ConvertRect(stp.RectArea()); _currentDoc.InitSecurityHandler(); //pdftron.PDF.Image img = pdftron.PDF.Image.Create(_currentDoc, String.IsNullOrEmpty(stp.ImagePath()) ? "SuccessStamp.jpg" : stp.ImagePath()); pdftron.PDF.Image img = pdftron.PDF.Image.Create(_currentDoc, System.Convert.FromBase64String(stp.Image())); s.SetTextAlignment(pdftron.PDF.Stamper.TextAlignment.e_align_center); s.SetAlignment(pdftron.PDF.Stamper.HorizontalAlignment.e_horizontal_left, pdftron.PDF.Stamper.VerticalAlignment.e_vertical_bottom); s.SetSize(pdftron.PDF.Stamper.SizeType.e_absolute_size, rect.x2 - rect.x1, rect.y2 - rect.y1); s.SetPosition(rect.x1, rect.y1); s.SetAsBackground(false); s.SetOpacity(.3); s.StampImage(_currentDoc, img, new PageSet(stp.Page())); } var annot = page.GetAnnot(page.GetNumAnnots() - 1); stp.RectArea(AnnotationsMannager.ConvertRect(annot.GetRect())); return(annot); }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; Console.WriteLine("_______________________________________________"); Console.WriteLine("Opening the input pdf..."); try // Test - Adjust the position of content within the page. { using (PDFDoc input_doc = new PDFDoc(input_path + "tiger.pdf")) { input_doc.InitSecurityHandler(); Page pg = input_doc.GetPage(1); Rect media_box = pg.GetMediaBox(); media_box.x1 -= 200; // translate the page 200 units (1 uint = 1/72 inch) media_box.x2 -= 200; media_box.Update(); input_doc.Save(output_path + "tiger_shift.pdf", 0); } Console.WriteLine("Done. Result saved in tiger_shift..."); } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
public void RevertTransportRect(int pageNum, Rect bounds) { Page page = _pdfDoc.GetPage(pageNum); Matrix2D matrix = page.GetDefaultMatrix().Inverse(); ApplyMatrixToRect(matrix, bounds); }
/// <summary> /// 產生PDF報表 /// </summary> /// <param name="set"></param> public static void PrintBill(/*BillSet set*/) { using PDFDoc pdfdoc = new PDFDoc(); //pdftron.PDF.Convert.OfficeToPDF(pdfdoc, $"{ReportTemplate.TemplatePath}{ReportTemplate.BillTemplate}.docx", null); Page page = pdfdoc.GetPage(1); ContentReplacer replacer = new ContentReplacer(); //SetData(); //foreach (string key in Dic.Keys) replacer.AddString(key, Dic[key]); replacer.Process(page); //pdfdoc.Save($"{ReportTemplate.TemplateOutputPath}{ReportTemplate.ReceiptTemplate}{ReportTemplate.Resx}.pdf", SDFDoc.SaveOptions.e_linearized); }
public void ExportPdf() { using PDFDoc pdfdoc = new PDFDoc(); Convert.OfficeToPDF(pdfdoc, $"{ReportTemplate.TemplatePath}{ReportTemplate.ReceiptTemplate}.docx", null); Page pg = pdfdoc.GetPage(1); ContentReplacer replacer = new ContentReplacer(); SetData(); foreach (string key in Dic.Keys) { replacer.AddString(key, Dic[key]); } replacer.Process(pg); pdfdoc.Save($"{ReportTemplate.TemplateOutputPath}{ReportTemplate.ReceiptTemplate}{ReportTemplate.Resx}.pdf", SDFDoc.SaveOptions.e_linearized); }
public static Pdf Convert(string path, bool savePdfStructureAsXml = false, string xmlName = "pdf.xml") { PDFNet.Initialize(); using (var doc = new PDFDoc(path)) { var result = new XDocument(new XElement("Pdf")); for (int i = 1; i <= doc.GetPageCount(); i++) { var page = doc.GetPage(i); using (var txt = new TextExtractor()) { txt.Begin(page); var text = txt.GetAsXML(TextExtractor.XMLOutputFlags.e_words_as_elements | TextExtractor.XMLOutputFlags.e_output_bbox); //combine words within a line (we don't need their position) var xml = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(text))); var lines = xml.Root.DescendantsAndSelf().Where(s => s.Name == "Line").ToArray(); foreach (var line in lines) { var t = String.Join(" ", line.DescendantsAndSelf("Word").Select(s => s.Value)); line.RemoveNodes(); line.SetValue(t); } result.Root.Add(xml.Root); } } //save the temporary xml just for debug purposes if (savePdfStructureAsXml) { var destinationPath = Path.GetDirectoryName(path) + xmlName; using (var writer = new XmlTextWriter(destinationPath, null)) { writer.Formatting = Formatting.Indented; result.Save(writer); } } using (var ms = new MemoryStream()) { result.Save(ms); ms.Seek(0, SeekOrigin.Begin); var serializer = new XmlSerializer(typeof(Pdf)); return((Pdf)serializer.Deserialize(ms)); } } }
string GetPdfPageContent(PDFDoc pdfDoc, int pageNumber) { List <PdfString> matchFuncLines = new List <PdfString>(); Page page = pdfDoc.GetPage(pageNumber); if (page == null) { return(null); } TextExtractor txt = new TextExtractor(); txt.Begin(page); TextExtractor.Line line; TextExtractor.Word word; string lineString = null; StringBuilder stringBuilder = new StringBuilder(); for (line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine()) { if (line.GetNumWords() == 0) { continue; } lineString = null; for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord()) { int sz = word.GetStringLen(); if (sz == 0) { continue; } lineString += word.GetString(); } if (string.IsNullOrEmpty(lineString)) { continue; } stringBuilder.Append(lineString); } txt.Dispose(); return(stringBuilder.ToString()); }
void CreateTablePosHighlight(TablePos tablePos, PDFDoc pdfDoc) { double lineWidth = 3; Page page = pdfDoc.GetPage(tablePos.PageNum); foreach (FormLine line in tablePos.HorizontialLines.Concat(tablePos.VerticalLines).SelectMany(pair => pair.Value)) { lineWidth = line.IsExistent ? 3 : 1; Rect rect = line.IsTransverseLine ? new Rect(line.StartPoint.x, line.StartPoint.y, line.EndPoint.x, line.StartPoint.y + lineWidth) : new Rect(line.StartPoint.x, line.StartPoint.y, line.StartPoint.x + lineWidth, line.EndPoint.y); PdfTronHelper pdfTronHelper = new PdfTronHelper(pdfDoc); pdfTronHelper.RevertTransportRect(tablePos.PageNum, rect); CreateHighlight(pdfDoc, page, rect); } }
public Bitmap ToBitmap(string pdfFile) { Bitmap bitmap = null; using (PDFDoc doc = new PDFDoc(pdfFile)) { foxit.common.ErrorCode error_code = doc.Load(null); if (error_code == foxit.common.ErrorCode.e_ErrSuccess) { // Console.WriteLine("The PDFDoc [{0}] Error: {1}\n", pdfFile, error_code); //} //else //{ int nPageCount = doc.GetPageCount(); for (int i = 0; i < nPageCount; i++) { using (PDFPage page = doc.GetPage(i)) { // Parse page. page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false); int width = (int)(page.GetWidth()) * 600 / 96; int height = (int)(page.GetHeight()) * 600 / 96; Matrix2D matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation()); // Prepare a bitmap for rendering. bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); bitmap.SetResolution((float)600, (float)600); using (Graphics draw = Graphics.FromImage(bitmap)) { draw.Clear(Color.White); // Render page foxit.common.Renderer render = new foxit.common.Renderer(bitmap, false); render.StartRender(page, matrix, null); } } } } } return(bitmap); }
public void ReadTextFromCoordinates(string input_path, int pagenumber, int urx, int ury, int llx, int lly) { PDFDoc doc = new PDFDoc(input_path); doc.InitSecurityHandler(); Page page = doc.GetPage(pagenumber); ElementReader reader = new ElementReader(); PageIterator itr = doc.GetPageIterator(); reader.Begin(itr.Current()); LowLevelTextExtractUtils u = new LowLevelTextExtractUtils(); //u.DumpAllText(reader); //ConsoleLog += u.ConsoleLog; //reader.End(); string field3 = u.ReadTextFromRect(page, new Rect(urx, ury, llx, lly), reader); ConsoleLog = field3; reader.Dispose(); doc.Close(); }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; try { using (PDFDoc doc = new PDFDoc()) using (ElementBuilder eb = new ElementBuilder()) // ElementBuilder is used to build new Element objects using (ElementWriter writer = new ElementWriter()) // ElementWriter is used to write Elements to the page { // Start a new page ------------------------------------ // Position an image stream on several places on the page Page page = doc.PageCreate(new Rect(0, 0, 612, 794)); writer.Begin(page); // begin writing to this page // Create an Image that can be reused multiple times in the document or // multiple on the same page. MappedFile img_file = new MappedFile(input_path + "peppers.jpg"); FilterReader img_data = new FilterReader(img_file); Image img = Image.Create(doc, img_data, 400, 600, 8, ColorSpace.CreateDeviceRGB(), Image.InputFilter.e_jpeg); Element element = eb.CreateImage(img, new Matrix2D(200, -145, 20, 300, 200, 150)); writer.WritePlacedElement(element); GState gstate = element.GetGState(); // use the same image (just change its matrix) gstate.SetTransform(200, 0, 0, 300, 50, 450); writer.WritePlacedElement(element); // use the same image again (just change its matrix). writer.WritePlacedElement(eb.CreateImage(img, 300, 600, 200, -150)); writer.End(); // save changes to the current page doc.PagePushBack(page); // Start a new page ------------------------------------ // Construct and draw a path object using different styles page = doc.PageCreate(new Rect(0, 0, 612, 794)); writer.Begin(page); // begin writing to this page eb.Reset(); // Reset GState to default eb.PathBegin(); // start constructing the path eb.MoveTo(306, 396); eb.CurveTo(681, 771, 399.75, 864.75, 306, 771); eb.CurveTo(212.25, 864.75, -69, 771, 306, 396); eb.ClosePath(); element = eb.PathEnd(); // the path is now finished element.SetPathFill(true); // the path should be filled // Set the path color space and color gstate = element.GetGState(); gstate.SetFillColorSpace(ColorSpace.CreateDeviceCMYK()); gstate.SetFillColor(new ColorPt(1, 0, 0, 0)); // cyan gstate.SetTransform(0.5, 0, 0, 0.5, -20, 300); writer.WritePlacedElement(element); // Draw the same path using a different stroke color element.SetPathStroke(true); // this path is should be filled and stroked gstate.SetFillColor(new ColorPt(0, 0, 1, 0)); // yellow gstate.SetStrokeColorSpace(ColorSpace.CreateDeviceRGB()); gstate.SetStrokeColor(new ColorPt(1, 0, 0)); // red gstate.SetTransform(0.5, 0, 0, 0.5, 280, 300); gstate.SetLineWidth(20); writer.WritePlacedElement(element); // Draw the same path with with a given dash pattern element.SetPathFill(false); // this path is should be only stroked gstate.SetStrokeColor(new ColorPt(0, 0, 1)); // blue gstate.SetTransform(0.5, 0, 0, 0.5, 280, 0); double[] dash_pattern = { 30 }; gstate.SetDashPattern(dash_pattern, 0); writer.WritePlacedElement(element); // Use the path as a clipping path writer.WriteElement(eb.CreateGroupBegin()); // Save the graphics state // Start constructing a new path (the old path was lost when we created // a new Element using CreateGroupBegin()). eb.PathBegin(); eb.MoveTo(306, 396); eb.CurveTo(681, 771, 399.75, 864.75, 306, 771); eb.CurveTo(212.25, 864.75, -69, 771, 306, 396); eb.ClosePath(); element = eb.PathEnd(); // path is now built element.SetPathClip(true); // this path is a clipping path element.SetPathStroke(true); // this path is should be filled and stroked gstate = element.GetGState(); gstate.SetTransform(0.5, 0, 0, 0.5, -20, 0); writer.WriteElement(element); writer.WriteElement(eb.CreateImage(img, 100, 300, 400, 600)); writer.WriteElement(eb.CreateGroupEnd()); // Restore the graphics state writer.End(); // save changes to the current page doc.PagePushBack(page); // Start a new page ------------------------------------ page = doc.PageCreate(new Rect(0, 0, 612, 794)); writer.Begin(page); // begin writing to this page eb.Reset(); // Reset GState to default // Begin writing a block of text element = eb.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_roman), 12); writer.WriteElement(element); string data = "Hello World!"; element = eb.CreateTextRun(data); element.SetTextMatrix(10, 0, 0, 10, 0, 600); element.GetGState().SetLeading(15); // Set the spacing between lines writer.WriteElement(element); writer.WriteElement(eb.CreateTextNewLine()); // New line element = eb.CreateTextRun(data); gstate = element.GetGState(); gstate.SetTextRenderMode(GState.TextRenderingMode.e_stroke_text); gstate.SetCharSpacing(-1.25); gstate.SetWordSpacing(-1.25); writer.WriteElement(element); writer.WriteElement(eb.CreateTextNewLine()); // New line element = eb.CreateTextRun(data); gstate = element.GetGState(); gstate.SetCharSpacing(0); gstate.SetWordSpacing(0); gstate.SetLineWidth(3); gstate.SetTextRenderMode(GState.TextRenderingMode.e_fill_stroke_text); gstate.SetStrokeColorSpace(ColorSpace.CreateDeviceRGB()); gstate.SetStrokeColor(new ColorPt(1, 0, 0)); // red gstate.SetFillColorSpace(ColorSpace.CreateDeviceCMYK()); gstate.SetFillColor(new ColorPt(1, 0, 0, 0)); // cyan writer.WriteElement(element); writer.WriteElement(eb.CreateTextNewLine()); // New line // Set text as a clipping path to the image. element = eb.CreateTextRun(data); gstate = element.GetGState(); gstate.SetTextRenderMode(GState.TextRenderingMode.e_clip_text); writer.WriteElement(element); // Finish the block of text writer.WriteElement(eb.CreateTextEnd()); // Draw an image that will be clipped by the above text writer.WriteElement(eb.CreateImage(img, 10, 100, 1300, 720)); writer.End(); // save changes to the current page doc.PagePushBack(page); // Start a new page ------------------------------------ // // The example illustrates how to embed the external font in a PDF document. // The example also shows how ElementReader can be used to copy and modify // Elements between pages. using (ElementReader reader = new ElementReader()) { // Start reading Elements from the last page. We will copy all Elements to // a new page but will modify the font associated with text. reader.Begin(doc.GetPage(doc.GetPageCount())); page = doc.PageCreate(new Rect(0, 0, 1300, 794)); writer.Begin(page); // begin writing to this page eb.Reset(); // Reset GState to default // Embed an external font in the document. Font font = Font.CreateTrueTypeFont(doc, input_path + "font.ttf"); while ((element = reader.Next()) != null) // Read page contents { if (element.GetType() == Element.Type.e_text) { element.GetGState().SetFont(font, 12); } writer.WriteElement(element); } reader.End(); writer.End(); // save changes to the current page doc.PagePushBack(page); // Start a new page ------------------------------------ // // The example illustrates how to embed the external font in a PDF document. // The example also shows how ElementReader can be used to copy and modify // Elements between pages. // Start reading Elements from the last page. We will copy all Elements to // a new page but will modify the font associated with text. reader.Begin(doc.GetPage(doc.GetPageCount())); page = doc.PageCreate(new Rect(0, 0, 1300, 794)); writer.Begin(page); // begin writing to this page eb.Reset(); // Reset GState to default // Embed an external font in the document. Font font2 = Font.CreateType1Font(doc, input_path + "Misc-Fixed.pfa"); while ((element = reader.Next()) != null) // Read page contents { if (element.GetType() == Element.Type.e_text) { element.GetGState().SetFont(font2, 12); } writer.WriteElement(element); } reader.End(); writer.End(); // save changes to the current page doc.PagePushBack(page); // Start a new page ------------------------------------ page = doc.PageCreate(); writer.Begin(page); // begin writing to this page eb.Reset(); // Reset GState to default // Begin writing a block of text element = eb.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_roman), 12); element.SetTextMatrix(1.5, 0, 0, 1.5, 50, 600); element.GetGState().SetLeading(15); // Set the spacing between lines writer.WriteElement(element); string para = "A PDF text object consists of operators that can show " + "text strings, move the text position, and set text state and certain " + "other parameters. In addition, there are three parameters that are " + "defined only within a text object and do not persist from one text " + "object to the next: Tm, the text matrix, Tlm, the text line matrix, " + "Trm, the text rendering matrix, actually just an intermediate result " + "that combines the effects of text state parameters, the text matrix " + "(Tm), and the current transformation matrix"; int para_end = para.Length; int text_run = 0; int text_run_end; double para_width = 300; // paragraph width is 300 units double cur_width = 0; while (text_run < para_end) { text_run_end = para.IndexOf(' ', text_run); if (text_run_end < 0) { text_run_end = para_end - 1; } string text = para.Substring(text_run, text_run_end - text_run + 1); element = eb.CreateTextRun(text); if (cur_width + element.GetTextLength() < para_width) { writer.WriteElement(element); cur_width += element.GetTextLength(); } else { writer.WriteElement(eb.CreateTextNewLine()); // New line text = para.Substring(text_run, text_run_end - text_run + 1); element = eb.CreateTextRun(text); cur_width = element.GetTextLength(); writer.WriteElement(element); } text_run = text_run_end + 1; } // ----------------------------------------------------------------------- // The following code snippet illustrates how to adjust spacing between // characters (text runs). element = eb.CreateTextNewLine(); writer.WriteElement(element); // Skip 2 lines writer.WriteElement(element); writer.WriteElement(eb.CreateTextRun("An example of space adjustments between inter-characters:")); writer.WriteElement(eb.CreateTextNewLine()); // Write string "AWAY" without space adjustments between characters. element = eb.CreateTextRun("AWAY"); writer.WriteElement(element); writer.WriteElement(eb.CreateTextNewLine()); // Write string "AWAY" with space adjustments between characters. element = eb.CreateTextRun("A"); writer.WriteElement(element); element = eb.CreateTextRun("W"); element.SetPosAdjustment(140); writer.WriteElement(element); element = eb.CreateTextRun("A"); element.SetPosAdjustment(140); writer.WriteElement(element); element = eb.CreateTextRun("Y again"); element.SetPosAdjustment(115); writer.WriteElement(element); // Draw the same strings using direct content output... writer.Flush(); // flush pending Element writing operations. // You can also write page content directly to the content stream using // ElementWriter.WriteString(...) and ElementWriter.WriteBuffer(...) methods. // Note that if you are planning to use these functions you need to be familiar // with PDF page content operators (see Appendix A in PDF Reference Manual). // Because it is easy to make mistakes during direct output we recommend that // you use ElementBuilder and Element interface instead. writer.WriteString("T* T* "); // New Lines // writer.WriteElement(eb.CreateTextNewLine()); writer.WriteString("(Direct output to PDF page content stream:) Tj T* "); writer.WriteString("(AWAY) Tj T* "); writer.WriteString("[(A)140(W)140(A)115(Y again)] TJ "); // Finish the block of text writer.WriteElement(eb.CreateTextEnd()); writer.End(); // save changes to the current page doc.PagePushBack(page); // Start a new page ------------------------------------ // Image Masks // // In the opaque imaging model, images mark all areas they occupy on the page as // if with opaque paint. All portions of the image, whether black, white, gray, // or color, completely obscure any marks that may previously have existed in the // same place on the page. // In the graphic arts industry and page layout applications, however, it is common // to crop or 'mask out' the background of an image and then place the masked image // on a different background, allowing the existing background to show through the // masked areas. This sample illustrates how to use image masks. page = doc.PageCreate(); writer.Begin(page); // begin writing to the page // Create the Image Mask MappedFile imgf = new MappedFile(input_path + "imagemask.dat"); FilterReader mask_read = new FilterReader(imgf); ColorSpace device_gray = ColorSpace.CreateDeviceGray(); Image mask = Image.Create(doc, mask_read, 64, 64, 1, device_gray, Image.InputFilter.e_ascii_hex); mask.GetSDFObj().PutBool("ImageMask", true); element = eb.CreateRect(0, 0, 612, 794); element.SetPathStroke(false); element.SetPathFill(true); element.GetGState().SetFillColorSpace(device_gray); element.GetGState().SetFillColor(new ColorPt(0.8)); writer.WritePlacedElement(element); element = eb.CreateImage(mask, new Matrix2D(200, 0, 0, -200, 40, 680)); element.GetGState().SetFillColor(new ColorPt(0.1)); writer.WritePlacedElement(element); element.GetGState().SetFillColorSpace(ColorSpace.CreateDeviceRGB()); element.GetGState().SetFillColor(new ColorPt(1, 0, 0)); element = eb.CreateImage(mask, new Matrix2D(200, 0, 0, -200, 320, 680)); writer.WritePlacedElement(element); element.GetGState().SetFillColor(new ColorPt(0, 1, 0)); element = eb.CreateImage(mask, new Matrix2D(200, 0, 0, -200, 40, 380)); writer.WritePlacedElement(element); { // This sample illustrates Explicit Masking. img = Image.Create(doc, input_path + "peppers.jpg"); // mask is the explicit mask for the primary (base) image img.SetMask(mask); element = eb.CreateImage(img, new Matrix2D(200, 0, 0, -200, 320, 380)); writer.WritePlacedElement(element); } writer.End(); // save changes to the current page doc.PagePushBack(page); // Transparency sample ---------------------------------- // Start a new page ------------------------------------- page = doc.PageCreate(); writer.Begin(page); // begin writing to this page eb.Reset(); // Reset the GState to default // Write some transparent text at the bottom of the page. element = eb.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_roman), 100); // Set the text knockout attribute. Text knockout must be set outside of // the text group. gstate = element.GetGState(); gstate.SetTextKnockout(false); gstate.SetBlendMode(GState.BlendMode.e_bl_difference); writer.WriteElement(element); element = eb.CreateTextRun("Transparency"); element.SetTextMatrix(1, 0, 0, 1, 30, 30); gstate = element.GetGState(); gstate.SetFillColorSpace(ColorSpace.CreateDeviceCMYK()); gstate.SetFillColor(new ColorPt(1, 0, 0, 0)); gstate.SetFillOpacity(0.5); writer.WriteElement(element); // Write the same text on top the old; shifted by 3 points element.SetTextMatrix(1, 0, 0, 1, 33, 33); gstate.SetFillColor(new ColorPt(0, 1, 0, 0)); gstate.SetFillOpacity(0.5); writer.WriteElement(element); writer.WriteElement(eb.CreateTextEnd()); // Draw three overlapping transparent circles. eb.PathBegin(); // start constructing the path eb.MoveTo(459.223, 505.646); eb.CurveTo(459.223, 415.841, 389.85, 343.04, 304.273, 343.04); eb.CurveTo(218.697, 343.04, 149.324, 415.841, 149.324, 505.646); eb.CurveTo(149.324, 595.45, 218.697, 668.25, 304.273, 668.25); eb.CurveTo(389.85, 668.25, 459.223, 595.45, 459.223, 505.646); element = eb.PathEnd(); element.SetPathFill(true); gstate = element.GetGState(); gstate.SetFillColorSpace(ColorSpace.CreateDeviceRGB()); gstate.SetFillColor(new ColorPt(0, 0, 1)); // Blue Circle gstate.SetBlendMode(GState.BlendMode.e_bl_normal); gstate.SetFillOpacity(0.5); writer.WriteElement(element); // Translate relative to the Blue Circle gstate.SetTransform(1, 0, 0, 1, 113, -185); gstate.SetFillColor(new ColorPt(0, 1, 0)); // Green Circle gstate.SetFillOpacity(0.5); writer.WriteElement(element); // Translate relative to the Green Circle gstate.SetTransform(1, 0, 0, 1, -220, 0); gstate.SetFillColor(new ColorPt(1, 0, 0)); // Red Circle gstate.SetFillOpacity(0.5); writer.WriteElement(element); writer.End(); // save changes to the current page doc.PagePushBack(page); // End page ------------------------------------ } doc.Save(output_path + "element_builder.pdf", SDFDoc.SaveOptions.e_remove_unused); Console.WriteLine("Done. Result saved in element_builder.pdf..."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
public void ReadAdvanced(string input_path) { PDFNet.Initialize(); try { PDFDoc doc = new PDFDoc(input_path); doc.InitSecurityHandler(); Page page = doc.GetPage(1); if (page == null) { ConsoleLog += "Page not found."; return; } TextExtractor txt = new TextExtractor(); txt.Begin(page); // Read the page. // Other options you may want to consider... // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_no_dup_remove); // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_remove_hidden_text); // ... // Example 1. Get all text on the page in a single string. // Words will be separated with space or new line characters. if (example1_basic) { // Get the word count. ConsoleLog += "Word Count: {0}" + txt.GetWordCount(); ConsoleLog += "\n\n- GetAsText --------------------------\n{0}" + txt.GetAsText(); ConsoleLog += "-----------------------------------------------------------"; } // Example 2. Get XML logical structure for the page. if (example2_xml) { String text = txt.GetAsXML(TextExtractor.XMLOutputFlags.e_words_as_elements | TextExtractor.XMLOutputFlags.e_output_bbox | TextExtractor.XMLOutputFlags.e_output_style_info); ConsoleLog += "\n\n- GetAsXML --------------------------\n{0}" + text; ConsoleLog += "-----------------------------------------------------------"; } // Example 3. Extract words one by one. if (example3_wordlist) { TextExtractor.Word word; for (TextExtractor.Line line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine()) { for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord()) { ConsoleLog += word.GetString(); } } ConsoleLog += "-----------------------------------------------------------"; } // Example 3. A more advanced text extraction example. // The output is XML structure containing paragraphs, lines, words, // as well as style and positioning information. if (example4_advanced) { Rect bbox; int cur_flow_id = -1, cur_para_id = -1; TextExtractor.Line line; TextExtractor.Word word; TextExtractor.Style s, line_style; // For each line on the page... for (line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine()) { if (line.GetNumWords() == 0) { continue; } if (cur_flow_id != line.GetFlowID()) { if (cur_flow_id != -1) { if (cur_para_id != -1) { cur_para_id = -1; ConsoleLog += "</Para>"; } ConsoleLog += "</Flow>"; } cur_flow_id = line.GetFlowID(); ConsoleLog += "<Flow id=\"{0}\">" + cur_flow_id; } if (cur_para_id != line.GetParagraphID()) { if (cur_para_id != -1) { ConsoleLog += "</Para>"; } cur_para_id = line.GetParagraphID(); ConsoleLog += "<Para id=\"{0}\">" + cur_para_id; } bbox = line.GetBBox(); line_style = line.GetStyle(); Console.Write("<Line box=\"" + bbox.y1 + "," + bbox.y2 + "," + bbox.x1 + "," + bbox.x2 + ">"); PrintStyle(line_style); ConsoleLog += ""; // For each word in the line... for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord()) { // Output the bounding box for the word. bbox = word.GetBBox(); ConsoleLog += "<Word box=\"{0}, {1}, {2}, {3}\"" + bbox.x1 + bbox.y1 + bbox.x2 + bbox.y2; int sz = word.GetStringLen(); if (sz == 0) { continue; } // If the word style is different from the parent style, output the new style. s = word.GetStyle(); if (s != line_style) { PrintStyle(s); } ConsoleLog += ">\n" + word.GetString(); ConsoleLog += "</Word>"; } ConsoleLog += "</Line>"; } if (cur_flow_id != -1) { if (cur_para_id != -1) { cur_para_id = -1; ConsoleLog += "</Para>"; } ConsoleLog += "</Flow>"; } } // Note: Calling Dispose() on TextExtractor when it is not anymore in use can result in increased performance and lower memory consumption. txt.Dispose(); doc.Close(); ConsoleLog += "Done."; } catch (PDFNetException e) { ConsoleLog += e.Message; } // Sample code showing how to use low-level text extraction APIs. if (example5_low_level) { try { LowLevelTextExtractUtils util = new LowLevelTextExtractUtils(); PDFDoc doc = new PDFDoc(input_path); doc.InitSecurityHandler(); // Example 1. Extract all text content from the document ElementReader reader = new ElementReader(); PageIterator itr = doc.GetPageIterator(); //for (; itr.HasNext(); itr.Next()) // Read every page { reader.Begin(itr.Current()); LowLevelTextExtractUtils u = new LowLevelTextExtractUtils(); u.DumpAllText(reader); ConsoleLog += u.ConsoleLog; reader.End(); } // Example 2. Extract text based on the selection rectangle. ConsoleLog += "----------------------------------------------------"; ConsoleLog += "Extract text based on the selection rectangle."; ConsoleLog += "----------------------------------------------------"; Page first_page = doc.GetPage(1); string field1 = util.ReadTextFromRect(first_page, new Rect(27, 392, 563, 534), reader); string field2 = util.ReadTextFromRect(first_page, new Rect(28, 551, 106, 623), reader); string field3 = util.ReadTextFromRect(first_page, new Rect(208, 550, 387, 621), reader); ConsoleLog += "Field 1: {0}" + field1; ConsoleLog += "Field 2: {0}" + field2; ConsoleLog += "Field 3: {0}" + field3; // ... reader.Dispose(); doc.Close(); ConsoleLog += "Done."; } catch (PDFNetException e) { ConsoleLog += e.Message; } } PDFNet.Terminate(); }
/// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { // The first step in every application using PDFNet is to initialize the // library and set the path to common PDF resources. The library is usually // initialized only once, but calling Initialize() multiple times is also fine. PDFNet.Initialize(); try { // Optional: Set ICC color profiles to fine tune color conversion // for PDF 'device' color spaces. You can use your own ICC profiles. // Standard Adobe color profiles can be download from Adobes site: // http://www.adobe.com/support/downloads/iccprofiles/iccprofiles_win.html // // Simply drop all *.icc files in PDFNet resource folder or you specify // the full pathname. //--- // PDFNet.SetResourcesPath("../../../../../resources"); // PDFNet.SetColorManagement(); // PDFNet.SetDefaultDeviceCMYKProfile("USWebCoatedSWOP.icc"); // will search in PDFNet resource folder. // PDFNet.SetDefaultDeviceRGBProfile("AdobeRGB1998.icc"); // Optional: Set predefined font mappings to override default font // substitution for documents with missing fonts. For example: //--- // PDFNet.AddFontSubst("StoneSans-Semibold", "C:/WINDOWS/Fonts/comic.ttf"); // PDFNet.AddFontSubst("StoneSans", "comic.ttf"); // search for 'comic.ttf' in PDFNet resource folder. // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf"); // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf"); // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf"); // // If fonts are in PDFNet resource folder, it is not necessary to specify // the full path name. For example, //--- // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Korea1, "AdobeMyungjoStd-Medium.otf"); // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_CNS1, "AdobeSongStd-Light.otf"); // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_GB1, "AdobeMingStd-Light.otf"); } catch (Exception) { Console.WriteLine("The specified color profile was not found."); } // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; using (PDFDraw draw = new PDFDraw()) { //-------------------------------------------------------------------------------- // Example 1) Convert the first PDF page to PNG at 92 DPI. // A three step tutorial to convert PDF page to an image. try { // A) Open the PDF document. using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf")) { // Initialize the security handler, in case the PDF is encrypted. doc.InitSecurityHandler(); // B) The output resolution is set to 92 DPI. draw.SetDPI(92); // C) Rasterize the first page in the document and save the result as PNG. Page pg = doc.GetPage(1); draw.Export(pg, output_path + "tiger_92dpi.png"); Console.WriteLine("Example 1: tiger_92dpi.png"); // Export the same page as TIFF draw.Export(pg, output_path + "tiger_92dpi.tif", "TIFF"); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 2) Convert the all pages in a given document to JPEG at 72 DPI. ObjSet hint_set = new ObjSet(); // A collection of rendering 'hits'. Console.WriteLine("Example 2:"); try { using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) { // Initialize the security handler, in case the PDF is encrypted. doc.InitSecurityHandler(); draw.SetDPI(72); // Set the output resolution is to 72 DPI. // Use optional encoder parameter to specify JPEG quality. Obj encoder_param = hint_set.CreateDict(); encoder_param.PutNumber("Quality", 80); // Traverse all pages in the document. for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next()) { string output_filename = string.Format("newsletter{0:d}.jpg", itr.GetPageNumber()); Console.WriteLine("newsletter{0:d}.jpg", itr.GetPageNumber()); draw.Export(itr.Current(), output_path + output_filename, "JPEG", encoder_param); } } Console.WriteLine("Done."); } catch (PDFNetException e) { Console.WriteLine(e.Message); } try // Examples 3-6 { // Common code for remaining samples. using (PDFDoc tiger_doc = new PDFDoc(input_path + "tiger.pdf")) { // Initialize the security handler, in case the PDF is encrypted. tiger_doc.InitSecurityHandler(); Page page = tiger_doc.GetPage(1); //-------------------------------------------------------------------------------- // Example 3) Convert the first page to GDI+ Bitmap. Also, rotate the // page 90 degrees and save the result as RAW. draw.SetDPI(100); // Set the output resolution is to 100 DPI. draw.SetRotate(Page.Rotate.e_90); // Rotate all pages 90 degrees clockwise. BitmapInfo buf = draw.GetBitmap(page, PDFDraw.PixelFormat.e_rgb, false); // Save the raw RGB data to disk. string filename = "tiger_100dpi_rot90.raw"; System.IO.File.WriteAllBytes(output_path + filename, buf.Buffer); Console.WriteLine("Example 3: tiger_100dpi_rot90.raw"); draw.SetRotate(Page.Rotate.e_0); // Disable image rotation for remaining samples. //-------------------------------------------------------------------------------- // Example 4) Convert PDF page to a fixed image size. Also illustrates some // other features in PDFDraw class such as rotation, image stretching, exporting // to grayscale, or monochrome. // Initialize render 'gray_hint' parameter, that is used to control the // rendering process. In this case we tell the rasterizer to export the image as // 1 Bit Per Component (BPC) image. Obj mono_hint = hint_set.CreateDict(); mono_hint.PutNumber("BPC", 1); // SetImageSize can be used instead of SetDPI() to adjust page scaling // dynamically so that given image fits into a buffer of given dimensions. draw.SetImageSize(1000, 1000); // Set the output image to be 1000 wide and 1000 pixels tall draw.Export(page, output_path + "tiger_1000x1000.png", "PNG", mono_hint); Console.WriteLine("Example 4: tiger_1000x1000.png"); draw.SetImageSize(200, 400); // Set the output image to be 200 wide and 300 pixels tall draw.SetRotate(Page.Rotate.e_180); // Rotate all pages 90 degrees clockwise. // 'gray_hint' tells the rasterizer to export the image as grayscale. Obj gray_hint = hint_set.CreateDict(); gray_hint.PutName("ColorSpace", "Gray"); draw.Export(page, output_path + "tiger_200x400_rot180.png", "PNG", gray_hint); Console.WriteLine("Example 4: tiger_200x400_rot180.png"); draw.SetImageSize(400, 200, false); // The third parameter sets 'preserve-aspect-ratio' to false. draw.SetRotate(Page.Rotate.e_0); // Disable image rotation. draw.Export(page, output_path + "tiger_400x200_stretch.jpg", "JPEG"); Console.WriteLine("Example 4: tiger_400x200_stretch.jpg"); //-------------------------------------------------------------------------------- // Example 5) Zoom into a specific region of the page and rasterize the // area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image). page.SetCropBox(new Rect(216, 522, 330, 600)); // Set the page crop box. // Select the crop region to be used for drawing. draw.SetPageBox(Page.Box.e_crop); draw.SetDPI(900); // Set the output image resolution to 900 DPI. draw.Export(page, output_path + "tiger_zoom_900dpi.png", "PNG"); Console.WriteLine("Example 5: tiger_zoom_900dpi.png"); // ------------------------------------------------------------------------------- // Example 6) draw.SetImageSize(50, 50); // Set the thumbnail to be 50x50 pixel image. draw.Export(page, output_path + "tiger_zoom_50x50.png", "PNG"); Console.WriteLine("Example 6: tiger_zoom_50x50.png"); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } Obj cmyk_hint = hint_set.CreateDict(); cmyk_hint.PutName("ColorSpace", "CMYK"); //-------------------------------------------------------------------------------- // Example 7) Convert the first PDF page to CMYK TIFF at 92 DPI. // A three step tutorial to convert PDF page to an image. try { // A) Open the PDF document. using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf")) { // Initialize the security handler, in case the PDF is encrypted. doc.InitSecurityHandler(); // B) The output resolution is set to 92 DPI. draw.SetDPI(92); // C) Rasterize the first page in the document and save the result as TIFF. Page pg = doc.GetPage(1); draw.Export(pg, output_path + "out1.tif", "TIFF", cmyk_hint); Console.WriteLine("Example 7: out1.tif"); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 8) Export raster content to PNG using different image smoothing settings. try { // A) Open the PDF document. using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf")) { // Initialize the security handler, in case the PDF is encrypted. doc.InitSecurityHandler(); // B) Get the page matrix Page pg = doc.GetPage(1); Page.Box box = Page.Box.e_crop; Matrix2D mtx = pg.GetDefaultMatrix(true, box); // We want to render a quadrant, so use half of width and height double pg_w = pg.GetPageWidth(box) / 2; double pg_h = pg.GetPageHeight(box) / 2; // C) Scale matrix from PDF space to buffer space double dpi = 96.0; double scale = dpi / 72.0; // PDF space is 72 dpi int buf_w = (int)(Math.Floor(scale * pg_w)); int buf_h = (int)(Math.Floor(scale * pg_h)); int bytes_per_pixel = 4; // BGRA buffer int buf_size = buf_w * buf_h * bytes_per_pixel; mtx.Translate(0, -pg_h); // translate by '-pg_h' since we want south-west quadrant mtx = new Matrix2D(scale, 0, 0, scale, 0, 0) * mtx; // D) Rasterize page into memory buffer, according to our parameters byte[] buf; PDFRasterizer rast = new PDFRasterizer(); buf = rast.Rasterize(pg, buf_w, buf_h, buf_w * bytes_per_pixel, bytes_per_pixel, true, mtx); // buf now contains raw BGRA bitmap. Console.WriteLine("Example 8: Successfully rasterized into memory buffer."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 9) Export raster content to PNG using different image smoothing settings. try { using (PDFDoc text_doc = new PDFDoc(input_path + "lorem_ipsum.pdf")) { text_doc.InitSecurityHandler(); draw.SetImageSmoothing(false, false); string filename = "raster_text_no_smoothing.png"; draw.Export(text_doc.GetPageIterator().Current(), output_path + filename); Console.WriteLine("Example 9 a): " + filename + ". Done."); filename = "raster_text_smoothed.png"; draw.SetImageSmoothing(true, false /*default quality bilinear resampling*/); draw.Export(text_doc.GetPageIterator().Current(), output_path + filename); Console.WriteLine("Example 9 b): " + filename + ". Done."); filename = "raster_text_high_quality.png"; draw.SetImageSmoothing(true, true /*high quality area resampling*/); draw.Export(text_doc.GetPageIterator().Current(), output_path + filename); Console.WriteLine("Example 9 c): " + filename + ". Done."); } } catch (Exception e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 10) Export separations directly, without conversion to an output colorspace try { using (PDFDoc separation_doc = new PDFDoc(input_path + "op_blend_test.pdf")) { separation_doc.InitSecurityHandler(); Obj separation_hint = hint_set.CreateDict(); separation_hint.PutName("ColorSpace", "Separation"); draw.SetDPI(96); draw.SetImageSmoothing(true, true); draw.SetOverprint(PDFRasterizer.OverprintPreviewMode.e_op_on); string filename = "merged_separations.png"; draw.Export(separation_doc.GetPageIterator().Current(), output_path + filename, "PNG"); Console.WriteLine("Example 10 a): " + filename + ". Done."); filename = "separation"; draw.Export(separation_doc.GetPageIterator().Current(), output_path + filename, "PNG", separation_hint); Console.WriteLine("Example 10 b): " + filename + "_[ink].png. Done."); filename = "separation_NChannel.tif"; draw.Export(separation_doc.GetPageIterator().Current(), output_path + filename, "TIFF", separation_hint); Console.WriteLine("Example 10 c): " + filename + ". Done."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } } // using PDFDraw }
private static void AnnotationHighLevelAPI(PDFDoc doc) { // The following code snippet traverses all annotations in the document System.Console.WriteLine("Traversing all annotations in the document..."); string uri; int page_num = 1; for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next()) { System.Console.WriteLine("Page " + page_num++ + ": "); Page page = itr.Current(); int num_annots = page.GetNumAnnots(); for (int i = 0; i < num_annots; ++i) { Annot annot = page.GetAnnot(i); if (!annot.IsValid()) { continue; } System.Console.WriteLine("Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName()); Rect bbox = annot.GetRect(); System.Console.WriteLine(" Position: " + bbox.x1 + ", " + bbox.y1 + ", " + bbox.x2 + ", " + bbox.y2); switch (annot.GetType()) { case Annot.Type.e_Link: { Link lnk = new Link(annot); Action action = lnk.GetAction(); if (!action.IsValid()) { continue; } if (action.GetType() == Action.Type.e_GoTo) { Destination dest = action.GetDest(); if (!dest.IsValid()) { System.Console.WriteLine(" Destination is not valid"); } else { int pg_num = dest.GetPage().GetIndex(); System.Console.WriteLine(" Links to: page number " + pg_num + " in this document"); } } else if (action.GetType() == Action.Type.e_URI) { uri = action.GetSDFObj().Get("URI").Value().GetAsPDFText(); System.Console.WriteLine(" Links to: " + uri); } // ... } break; case Annot.Type.e_Widget: break; case Annot.Type.e_FileAttachment: break; // ... default: break; } } } // Use the high-level API to create new annotations. Page first_page = doc.GetPage(1); // Create a hyperlink... Link hyperlink = Link.Create(doc, new Rect(85, 570, 503, 524), Action.CreateURI(doc, "http://www.pdftron.com")); first_page.AnnotPushBack(hyperlink); // Create an intra-document link... Action goto_page_3 = Action.CreateGoto(Destination.CreateFitH(doc.GetPage(3), 0)); Link link = Link.Create(doc, new Rect(85, 458, 503, 502), goto_page_3); // Set the annotation border width to 3 points... Annot.BorderStyle border_style = new Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 3, 0, 0); //link.SetBorderStyle(border_style); link.SetColor(new ColorPt(0, 0, 1)); // Add the new annotation to the first page first_page.AnnotPushBack(link); // Create a stamp annotation ... RubberStamp stamp = RubberStamp.Create(doc, new Rect(30, 30, 300, 200)); stamp.SetIcon("Draft"); first_page.AnnotPushBack(stamp); // Create a file attachment annotation (embed the 'peppers.jpg'). FileAttachment file_attach = FileAttachment.Create(doc, new Rect(80, 280, 200, 320), (input_path + "peppers.jpg")); first_page.AnnotPushBack(file_attach); Ink ink = Ink.Create(doc, new Rect(110, 10, 300, 200)); Point pt3 = new Point(110, 10); //pt3.x = 110; pt3.y = 10; ink.SetPoint(0, 0, pt3); pt3.x = 150; pt3.y = 50; ink.SetPoint(0, 1, pt3); pt3.x = 190; pt3.y = 60; ink.SetPoint(0, 2, pt3); pt3.x = 180; pt3.y = 90; ink.SetPoint(1, 0, pt3); pt3.x = 190; pt3.y = 95; ink.SetPoint(1, 1, pt3); pt3.x = 200; pt3.y = 100; ink.SetPoint(1, 2, pt3); pt3.x = 166; pt3.y = 86; ink.SetPoint(2, 0, pt3); pt3.x = 196; pt3.y = 96; ink.SetPoint(2, 1, pt3); pt3.x = 221; pt3.y = 121; ink.SetPoint(2, 2, pt3); pt3.x = 288; pt3.y = 188; ink.SetPoint(2, 3, pt3); ink.SetColor(new ColorPt(0, 1, 1), 3); first_page.AnnotPushBack(ink); }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; bool example1_basic = false; bool example2_xml = false; bool example3_wordlist = false; bool example4_advanced = true; bool example5_low_level = false; // Sample code showing how to use high-level text extraction APIs. try { using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) { doc.InitSecurityHandler(); Page page = doc.GetPage(1); if (page == null) { Console.WriteLine("Page not found."); return; } using (TextExtractor txt = new TextExtractor()) { txt.Begin(page); // Read the page. // Other options you may want to consider... // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_no_dup_remove); // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_remove_hidden_text); // ... // Example 1. Get all text on the page in a single string. // Words will be separated with space or new line characters. if (example1_basic) { // Get the word count. Console.WriteLine("Word Count: {0}", txt.GetWordCount()); Console.WriteLine("\n\n- GetAsText --------------------------\n{0}", txt.GetAsText()); Console.WriteLine("-----------------------------------------------------------"); } // Example 2. Get XML logical structure for the page. if (example2_xml) { String text = txt.GetAsXML(TextExtractor.XMLOutputFlags.e_words_as_elements | TextExtractor.XMLOutputFlags.e_output_bbox | TextExtractor.XMLOutputFlags.e_output_style_info); Console.WriteLine("\n\n- GetAsXML --------------------------\n{0}", text); Console.WriteLine("-----------------------------------------------------------"); } // Example 3. Extract words one by one. if (example3_wordlist) { TextExtractor.Word word; for (TextExtractor.Line line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine()) { for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord()) { Console.WriteLine(word.GetString()); } } Console.WriteLine("-----------------------------------------------------------"); } // Example 3. A more advanced text extraction example. // The output is XML structure containing paragraphs, lines, words, // as well as style and positioning information. if (example4_advanced) { Rect bbox; int cur_flow_id = -1, cur_para_id = -1; TextExtractor.Line line; TextExtractor.Word word; TextExtractor.Style s, line_style; Console.WriteLine("<PDFText>"); // For each line on the page... for (line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine()) { if (line.GetNumWords() == 0) { continue; } if (cur_flow_id != line.GetFlowID()) { if (cur_flow_id != -1) { if (cur_para_id != -1) { cur_para_id = -1; Console.WriteLine("</Para>"); } Console.WriteLine("</Flow>"); } cur_flow_id = line.GetFlowID(); Console.WriteLine("<Flow id=\"{0}\">", cur_flow_id); } if (cur_para_id != line.GetParagraphID()) { if (cur_para_id != -1) { Console.WriteLine("</Para>"); } cur_para_id = line.GetParagraphID(); Console.WriteLine("<Para id=\"{0}\">", cur_para_id); } bbox = line.GetBBox(); line_style = line.GetStyle(); Console.Write("<Line box=\"{0}, {1}, {2}, {3}\"", bbox.x1.ToString("0.00"), bbox.y1.ToString("0.00"), bbox.x2.ToString("0.00"), bbox.y2.ToString("0.00")); PrintStyle(line_style); Console.Write(" cur_num=\"" + line.GetCurrentNum() + "\"" + ">\n"); // For each word in the line... for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord()) { // Output the bounding box for the word. bbox = word.GetBBox(); Console.Write("<Word box=\"{0}, {1}, {2}, {3}\"", bbox.x1.ToString("0.00"), bbox.y1.ToString("0.00"), bbox.x2.ToString("0.00"), bbox.y2.ToString("0.00")); Console.Write(" cur_num=\"" + word.GetCurrentNum() + "\""); int sz = word.GetStringLen(); if (sz == 0) { continue; } // If the word style is different from the parent style, output the new style. s = word.GetStyle(); if (s != line_style) { PrintStyle(s); } Console.Write(">{0}", word.GetString()); Console.WriteLine("</Word>"); } Console.WriteLine("</Line>"); } if (cur_flow_id != -1) { if (cur_para_id != -1) { cur_para_id = -1; Console.WriteLine("</Para>"); } Console.WriteLine("</Flow>"); } } } Console.WriteLine("</PDFText>"); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } // Sample code showing how to use low-level text extraction APIs. if (example5_low_level) { try { LowLevelTextExtractUtils util = new LowLevelTextExtractUtils(); using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) { doc.InitSecurityHandler(); // Example 1. Extract all text content from the document using (ElementReader reader = new ElementReader()) { PageIterator itr = doc.GetPageIterator(); //for (; itr.HasNext(); itr.Next()) // Read every page { reader.Begin(itr.Current()); LowLevelTextExtractUtils.DumpAllText(reader); reader.End(); } // Example 2. Extract text based on the selection rectangle. Console.WriteLine("----------------------------------------------------"); Console.WriteLine("Extract text based on the selection rectangle."); Console.WriteLine("----------------------------------------------------"); Page first_page = doc.GetPage(1); string field1 = util.ReadTextFromRect(first_page, new Rect(27, 392, 563, 534), reader); string field2 = util.ReadTextFromRect(first_page, new Rect(28, 551, 106, 623), reader); string field3 = util.ReadTextFromRect(first_page, new Rect(208, 550, 387, 621), reader); Console.WriteLine("Field 1: {0}", field1); Console.WriteLine("Field 2: {0}", field2); Console.WriteLine("Field 3: {0}", field3); // ... Console.WriteLine("Done."); } } } catch (PDFNetException e) { Console.WriteLine(e.Message); } } }
static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfiguration configuration = builder.Build(); Log.Logger = new LoggerConfiguration() .WriteTo.File("ExtractPDF.log") .ReadFrom.Configuration(configuration) .CreateLogger(); var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection, configuration); var serviceProvider = serviceCollection.BuildServiceProvider(); var logger = serviceProvider.GetService <ILogger <Program> >(); var filepath = configuration.GetSection("PDFConfig").GetValue <string>("Path"); PDFNet.Initialize(); // it is container of data. it will be used in parsing engine. List <Helper.LineDataOfPage> lstPages = new List <Helper.LineDataOfPage>(); using (PDFDoc doc = new PDFDoc(filepath)) //using (PDFDoc doc = new PDFDoc("Article Dev Data Set.pdf")) { doc.InitSecurityHandler(); int pagecount = doc.GetPageCount(); for (int i = 1; i <= pagecount; i++) { Page page = doc.GetPage(i); //Get Page Data if (page == null) { logger.LogError("Page not found."); continue; } using (TextExtractor txt = new TextExtractor()) { txt.Begin(page); // Read the page. // Get XML Content from the page String text = txt.GetAsXML(TextExtractor.XMLOutputFlags.e_words_as_elements | TextExtractor.XMLOutputFlags.e_output_bbox | TextExtractor.XMLOutputFlags.e_output_style_info); XmlSerializer serializer = new XmlSerializer(typeof(Helper.Page)); Helper.Page pg; using (TextReader reader = new StringReader(text)) { // Deserializing XML content. pg = (Helper.Page)serializer.Deserialize(reader); // add pre-processed data to container. lstPages.Add(Helper.Helper.ConvertPageToProcessingLineData(pg)); } } } } // Run parsing module. ParsingService ps = serviceProvider.GetService <ParsingService>(); ps.Process(lstPages); }
static void Main(string[] args) { PDFNet.Initialize(); try { using (PDFDoc doc = new PDFDoc()) using (ElementBuilder builder = new ElementBuilder()) // ElementBuilder is used to build new Element objects using (ElementWriter writer = new ElementWriter()) // ElementWriter is used to write Elements to the page { // Create three layers... Group image_layer = CreateLayer(doc, "Image Layer"); Group text_layer = CreateLayer(doc, "Text Layer"); Group vector_layer = CreateLayer(doc, "Vector Layer"); // Start a new page ------------------------------------ Page page = doc.PageCreate(); writer.Begin(page); // begin writing to this page // Add new content to the page and associate it with one of the layers. Element element = builder.CreateForm(CreateGroup1(doc, image_layer.GetSDFObj())); writer.WriteElement(element); element = builder.CreateForm(CreateGroup2(doc, vector_layer.GetSDFObj())); writer.WriteElement(element); // Add the text layer to the page... bool enableOCMD = false; // set to 'true' to enable 'ocmd' example. if (enableOCMD) { // A bit more advanced example of how to create an OCMD text layer that // is visible only if text, image and path layers are all 'ON'. // An example of how to set 'Visibility Policy' in OCMD. Obj ocgs = doc.CreateIndirectArray(); ocgs.PushBack(image_layer.GetSDFObj()); ocgs.PushBack(vector_layer.GetSDFObj()); ocgs.PushBack(text_layer.GetSDFObj()); OCMD text_ocmd = OCMD.Create(doc, ocgs, OCMD.VisibilityPolicyType.e_AllOn); element = builder.CreateForm(CreateGroup3(doc, text_ocmd.GetSDFObj())); } else { element = builder.CreateForm(CreateGroup3(doc, text_layer.GetSDFObj())); } writer.WriteElement(element); // Add some content to the page that does not belong to any layer... // In this case this is a rectangle representing the page border. element = builder.CreateRect(0, 0, page.GetPageWidth(), page.GetPageHeight()); element.SetPathFill(false); element.SetPathStroke(true); element.GetGState().SetLineWidth(40); writer.WriteElement(element); writer.End(); // save changes to the current page doc.PagePushBack(page); // Set the default viewing preference to display 'Layer' tab. PDFDocViewPrefs prefs = doc.GetViewPrefs(); prefs.SetPageMode(PDFDocViewPrefs.PageMode.e_UseOC); doc.Save(output_path + "pdf_layers.pdf", SDFDoc.SaveOptions.e_linearized); Console.WriteLine("Done."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } // The following is a code snippet shows how to selectively render // and export PDF layers. try { using (PDFDoc doc = new PDFDoc(output_path + "pdf_layers.pdf")) { doc.InitSecurityHandler(); if (!doc.HasOC()) { Console.WriteLine("The document does not contain 'Optional Content'"); } else { Config init_cfg = doc.GetOCGConfig(); Context ctx = new Context(init_cfg); using (PDFDraw pdfdraw = new PDFDraw()) { pdfdraw.SetImageSize(1000, 1000); pdfdraw.SetOCGContext(ctx); // Render the page using the given OCG context. Page page = doc.GetPage(1); // Get the first page in the document. pdfdraw.Export(page, output_path + "pdf_layers_default.png"); // Disable drawing of content that is not optional (i.e. is not part of any layer). ctx.SetNonOCDrawing(false); // Now render each layer in the input document to a separate image. Obj ocgs = doc.GetOCGs(); // Get the array of all OCGs in the document. if (ocgs != null) { int i, sz = ocgs.Size(); for (i = 0; i < sz; ++i) { Group ocg = new Group(ocgs.GetAt(i)); ctx.ResetStates(false); ctx.SetState(ocg, true); string fname = "pdf_layers_" + ocg.GetName() + ".png"; Console.WriteLine(fname); pdfdraw.Export(page, fname); } } // Now draw content that is not part of any layer... ctx.SetNonOCDrawing(true); ctx.SetOCDrawMode(Context.OCDrawMode.e_NoOC); pdfdraw.Export(page, output_path + "pdf_layers_non_oc.png"); Console.WriteLine("Done."); } } } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; // Sample 1 - Split a PDF document into multiple pages try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 1 - Split a PDF document into multiple pages..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) { in_doc.InitSecurityHandler(); int page_num = in_doc.GetPageCount(); for (int i = 1; i <= page_num; ++i) { using (PDFDoc new_doc = new PDFDoc()) { new_doc.InsertPages(0, in_doc, i, i, PDFDoc.InsertFlag.e_none); new_doc.Save(output_path + "newsletter_split_page_" + i + ".pdf", SDFDoc.SaveOptions.e_remove_unused); Console.WriteLine("Done. Result saved in newsletter_split_page_" + i + ".pdf"); } } } } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 2 - Merge several PDF documents into one try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 2 - Merge several PDF documents into one..."); using (PDFDoc new_doc = new PDFDoc()) { new_doc.InitSecurityHandler(); int page_num = 15; for (int i = 1; i <= page_num; ++i) { Console.WriteLine("Opening newsletter_split_page_" + i + ".pdf"); using (PDFDoc in_doc = new PDFDoc(output_path + "newsletter_split_page_" + i + ".pdf")) { new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDoc.InsertFlag.e_none); } } new_doc.Save(output_path + "newsletter_merge_pages.pdf", SDFDoc.SaveOptions.e_remove_unused); } Console.WriteLine("Done. Result saved in newsletter_merge_pages.pdf"); } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 3 - Delete every second page try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 3 - Delete every second page..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) { in_doc.InitSecurityHandler(); int page_num = in_doc.GetPageCount(); PageIterator itr; while (page_num >= 1) { itr = in_doc.GetPageIterator(page_num); in_doc.PageRemove(itr); page_num -= 2; } in_doc.Save(output_path + "newsletter_page_remove.pdf", 0); } Console.WriteLine("Done. Result saved in newsletter_page_remove.pdf..."); } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 4 - Inserts a page from one document at different // locations within another document try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 4 - Insert a page at different locations..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc in1_doc = new PDFDoc(input_path + "newsletter.pdf")) using (PDFDoc in2_doc = new PDFDoc(input_path + "fish.pdf")) { in1_doc.InitSecurityHandler(); in2_doc.InitSecurityHandler(); Page src_page = in2_doc.GetPage(1); PageIterator dst_page = in1_doc.GetPageIterator(1); int page_num = 1; while (dst_page.HasNext()) { if (page_num++ % 3 == 0) { in1_doc.PageInsert(dst_page, src_page); } dst_page.Next(); } in1_doc.Save(output_path + "newsletter_page_insert.pdf", 0); Console.WriteLine("Done. Result saved in newsletter_page_insert.pdf..."); } } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 5 - Replicate pages within a single document try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 5 - Replicate pages within a single document..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) { doc.InitSecurityHandler(); // Replicate the cover page three times (copy page #1 and place it before the // seventh page in the document page sequence) Page cover = doc.GetPage(1); PageIterator p7 = doc.GetPageIterator(7); doc.PageInsert(p7, cover); doc.PageInsert(p7, cover); doc.PageInsert(p7, cover); // Replicate the cover page two more times by placing it before and after // existing pages. doc.PagePushFront(cover); doc.PagePushBack(cover); doc.Save(output_path + "newsletter_page_clone.pdf", 0); Console.WriteLine("Done. Result saved in newsletter_page_clone.pdf..."); } } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 6 - Use ImportPages() in order to copy multiple pages at once // in order to preserve shared resources between pages (e.g. images, fonts, // colorspaces, etc.) try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 6 - Preserving shared resources using ImportPages..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) { in_doc.InitSecurityHandler(); using (PDFDoc new_doc = new PDFDoc()) { ArrayList copy_pages = new ArrayList(); for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next()) { copy_pages.Add(itr.Current()); } ArrayList imported_pages = new_doc.ImportPages(copy_pages); for (int i = 0; i != imported_pages.Count; ++i) { new_doc.PagePushFront((Page)imported_pages[i]); // Order pages in reverse order. // Use PagePushBack() if you would like to preserve the same order. } new_doc.Save(output_path + "newsletter_import_pages.pdf", 0); Console.WriteLine("Done. Result saved in newsletter_import_pages.pdf..."); Console.WriteLine(); Console.WriteLine("Note that the output file size is less than half the size"); Console.WriteLine("of the file produced using individual page copy operations"); Console.WriteLine("between two documents"); } } } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } }
/// <summary> // The following sample shows how to add new content (or watermark) PDF pages // using 'pdftron.PDF.Stamper' utility class. // // Stamper can be used to PDF pages with text, images, or with other PDF content // in only a few lines of code. Although Stamper is very simple to use compared // to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you // need full control over PDF creation use ElementBuilder/ElementWriter to add // new content to existing PDF pages as shown in the ElementBuilder sample project. /// </summary> static void Main(string[] args) { PDFNet.Initialize(); string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; string input_filename = "newsletter"; //-------------------------------------------------------------------------------- // Example 1) Add text stamp to all pages, then remove text stamp from odd pages. try { using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 0.5, 0.5)) { doc.InitSecurityHandler(); s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_center); s.SetFontColor(new ColorPt(1, 0, 0)); // set text color to red s.StampText(doc, "If you are reading this\nthis is an even page", new PageSet(1, doc.GetPageCount())); //delete all text stamps in odd pages Stamper.DeleteStamps(doc, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_odd)); doc.Save(output_path + input_filename + ".ex1.pdf", SDFDoc.SaveOptions.e_linearized); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 2) Add Image stamp to first 2 pages. try { using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .05, .05)) { doc.InitSecurityHandler(); Image img = Image.Create(doc, input_path + "peppers.jpg"); s.SetSize(Stamper.SizeType.e_relative_scale, 0.5, 0.5); //set position of the image to the center, left of PDF pages s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_center); s.SetFontColor(new ColorPt(0, 0, 0, 0)); s.SetRotation(180); s.SetAsBackground(false); //only stamp first 2 pages s.StampImage(doc, img, new PageSet(1, 2)); doc.Save(output_path + input_filename + ".ex2.pdf", SDFDoc.SaveOptions.e_linearized); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 3) Add Page stamp to all pages. try { using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) using (PDFDoc fish_doc = new PDFDoc(input_path + "fish.pdf")) using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .5, .5)) { doc.InitSecurityHandler(); fish_doc.InitSecurityHandler(); Page src_page = fish_doc.GetPage(1); Rect page_one_crop = src_page.GetCropBox(); // set size of the image to 10% of the original while keep the old aspect ratio s.SetSize(Stamper.SizeType.e_absolute_size, page_one_crop.Width() * 0.1, -1); s.SetOpacity(0.4); s.SetRotation(-67); //put the image at the bottom right hand corner s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom); s.StampPage(doc, src_page, new PageSet(1, doc.GetPageCount())); doc.Save(output_path + input_filename + ".ex3.pdf", SDFDoc.SaveOptions.e_linearized); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 4) Add Image stamp to first 20 odd pages. try { using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) using (Stamper s = new Stamper(Stamper.SizeType.e_absolute_size, 20, 20)) { doc.InitSecurityHandler(); s.SetOpacity(1); s.SetRotation(45); s.SetAsBackground(true); s.SetPosition(30, 40); Image img = Image.Create(doc, input_path + "peppers.jpg"); s.StampImage(doc, img, new PageSet(1, 20, PageSet.Filter.e_odd)); doc.Save(output_path + input_filename + ".ex4.pdf", SDFDoc.SaveOptions.e_linearized); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 5) Add text stamp to first 20 even pages try { using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .05, .05)) { doc.InitSecurityHandler(); s.SetPosition(0, 0); s.SetOpacity(0.7); s.SetRotation(90); s.SetSize(Stamper.SizeType.e_font_size, 80, -1); s.SetTextAlignment(Stamper.TextAlignment.e_align_center); s.StampText(doc, "Goodbye\nMoon", new PageSet(1, 20, PageSet.Filter.e_even)); doc.Save(output_path + input_filename + ".ex5.pdf", SDFDoc.SaveOptions.e_linearized); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 6) Add first page as stamp to all even pages try { using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) using (PDFDoc fish_doc = new PDFDoc(input_path + "fish.pdf")) using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .3, .3)) { doc.InitSecurityHandler(); fish_doc.InitSecurityHandler(); s.SetOpacity(1); s.SetRotation(270); s.SetAsBackground(true); s.SetPosition(0.5, 0.5, true); s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_bottom); Page page_one = fish_doc.GetPage(1); s.StampPage(doc, page_one, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_even)); doc.Save(output_path + input_filename + ".ex6.pdf", SDFDoc.SaveOptions.e_linearized); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 7) Add image stamp at top right corner in every pages try { using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .1, .1)) { doc.InitSecurityHandler(); s.SetOpacity(0.8); s.SetRotation(135); s.SetAsBackground(false); s.ShowsOnPrint(false); s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_top); s.SetPosition(10, 10); Image img = Image.Create(doc, input_path + "peppers.jpg"); s.StampImage(doc, img, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_all)); doc.Save(output_path + input_filename + ".ex7.pdf", SDFDoc.SaveOptions.e_linearized); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //-------------------------------------------------------------------------------- // Example 8) Add Text stamp to first 2 pages, and image stamp to first page. // Because text stamp is set as background, the image is top of the text // stamp. Text stamp on the first page is not visible. try { using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 0.07, -0.1)) { doc.InitSecurityHandler(); s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom); s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_top); s.SetFont(Font.Create(doc, Font.StandardType1Font.e_courier, true)); s.SetFontColor(new ColorPt(1, 0, 0, 0)); //set color to red s.SetTextAlignment(Stamper.TextAlignment.e_align_right); s.SetAsBackground(true); //set text stamp as background s.StampText(doc, "This is a title!", new PageSet(1, 2)); Image img = Image.Create(doc, input_path + "peppers.jpg"); s.SetAsBackground(false); // set image stamp as foreground s.StampImage(doc, img, new PageSet(1)); doc.Save(output_path + input_filename + ".ex8.pdf", SDFDoc.SaveOptions.e_linearized); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; // The following example illustrates how to replace an image in a certain region, // and how to change template text. try { using (PDFDoc doc = new PDFDoc(input_path + "BusinessCardTemplate.pdf")) using (ContentReplacer replacer = new ContentReplacer()) { doc.InitSecurityHandler(); // first, replace the image on the first page Page page = doc.GetPage(1); Image img = Image.Create(doc, input_path + "peppers.jpg"); replacer.AddImage(page.GetMediaBox(), img.GetSDFObj()); // next, replace the text place holders on the second page replacer.AddString("NAME", "John Smith"); replacer.AddString("QUALIFICATIONS", "Philosophy Doctor"); replacer.AddString("JOB_TITLE", "Software Developer"); replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd"); replacer.AddString("ADDRESS_LINE2", "Vancouver, BC"); replacer.AddString("PHONE_OFFICE", "604-730-8989"); replacer.AddString("PHONE_MOBILE", "604-765-4321"); replacer.AddString("EMAIL", "*****@*****.**"); replacer.AddString("WEBSITE_URL", "http://www.pdftron.com"); // finally, apply replacer.Process(page); doc.Save(output_path + "BusinessCard.pdf", 0); Console.WriteLine("Done. Result saved in BusinessCard.pdf"); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } // The following example illustrates how to replace text in a given region try { using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) using (ContentReplacer replacer = new ContentReplacer()) { doc.InitSecurityHandler(); Page page = doc.GetPage(1); Rect target_region = page.GetMediaBox(); string replacement_text = "hello hello hello hello hello hello hello hello hello hello"; replacer.AddText(target_region, replacement_text); replacer.Process(page); doc.Save(output_path + "ContentReplaced.pdf", 0); Console.WriteLine("Done. Result saved in ContentReplaced.pdf"); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } Console.WriteLine("Done."); }
static void Main(string[] args) { // Initialize PDFNetC PDFNet.Initialize(); bool result = true; //////////////////// TEST 0: /* Create an approval signature field that we can sign after certifying. * (Must be done before calling CertifyOnNextSave/SignOnNextSave/WithCustomHandler.) */ try { using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf")) { DigitalSignatureField approval_signature_field = doc.CreateDigitalSignatureField("PDFTronApprovalSig"); SignatureWidget widgetAnnotApproval = SignatureWidget.Create(doc, new Rect(300, 300, 500, 200), approval_signature_field); Page page1 = doc.GetPage(1); page1.AnnotPushBack(widgetAnnotApproval); doc.Save(output_path + "tiger_withApprovalField_output.pdf", SDFDoc.SaveOptions.e_remove_unused); } } catch (Exception e) { Console.Error.WriteLine(e); result = false; } //////////////////// TEST 1: certify a PDF. try { CertifyPDF(input_path + "tiger_withApprovalField.pdf", "PDFTronCertificationSig", input_path + "pdftron.pfx", "password", input_path + "pdftron.bmp", output_path + "tiger_withApprovalField_certified_output.pdf"); PrintSignaturesInfo(output_path + "tiger_withApprovalField_certified_output.pdf"); } catch (Exception e) { Console.Error.WriteLine(e); result = false; } //////////////////// TEST 2: sign a PDF with a certification and an unsigned signature field in it. try { SignPDF(input_path + "tiger_withApprovalField_certified.pdf", "PDFTronApprovalSig", input_path + "pdftron.pfx", "password", input_path + "signature.jpg", output_path + "tiger_withApprovalField_certified_approved_output.pdf"); PrintSignaturesInfo(output_path + "tiger_withApprovalField_certified_approved_output.pdf"); } catch (Exception e) { Console.Error.WriteLine(e); result = false; } //////////////////// TEST 3: Clear a certification from a document that is certified and has two approval signatures. try { ClearSignature(input_path + "tiger_withApprovalField_certified_approved.pdf", "PDFTronCertificationSig", output_path + "tiger_withApprovalField_certified_approved_certcleared_output.pdf"); PrintSignaturesInfo(output_path + "tiger_withApprovalField_certified_approved_certcleared_output.pdf"); } catch (Exception e) { Console.Error.WriteLine(e); result = false; } //////////////////// End of tests. //////////////////// if (result) { Console.Out.WriteLine("Tests successful.\n=========="); } else { Console.Out.WriteLine("Tests FAILED!!!\n=========="); } }
static void CertifyPDF(string in_docpath, string in_cert_field_name, string in_private_key_file_path, string in_keyfile_password, string in_appearance_image_path, string in_outpath) { Console.Out.WriteLine("================================================================================"); Console.Out.WriteLine("Certifying PDF document"); // Open an existing PDF using (PDFDoc doc = new PDFDoc(in_docpath)) { Console.Out.WriteLine("PDFDoc has " + (doc.HasSignatures() ? "signatures" : "no signatures")); Page page1 = doc.GetPage(1); // Create a random text field that we can lock using the field permissions feature. TextWidget annot1 = TextWidget.Create(doc, new Rect(50, 550, 350, 600), "asdf_test_field"); page1.AnnotPushBack(annot1); /* Create new signature form field in the PDFDoc. The name argument is optional; * leaving it empty causes it to be auto-generated. However, you may need the name for later. * Acrobat doesn't show digsigfield in side panel if it's without a widget. Using a * Rect with 0 width and 0 height, or setting the NoPrint/Invisible flags makes it invisible. */ DigitalSignatureField certification_sig_field = doc.CreateDigitalSignatureField(in_cert_field_name); SignatureWidget widgetAnnot = SignatureWidget.Create(doc, new Rect(0, 100, 200, 150), certification_sig_field); page1.AnnotPushBack(widgetAnnot); // (OPTIONAL) Add an appearance. // Widget AP from image Image img = Image.Create(doc, in_appearance_image_path); widgetAnnot.CreateSignatureAppearance(img); // End of optional appearance-adding code. // Add permissions. Lock the random text field. Console.Out.WriteLine("Adding document permissions."); certification_sig_field.SetDocumentPermissions(DigitalSignatureField.DocumentPermissions.e_annotating_formfilling_signing_allowed); Console.Out.WriteLine("Adding field permissions."); string[] fields_to_lock = new string[1]; fields_to_lock[0] = "asdf_test_field"; certification_sig_field.SetFieldPermissions(DigitalSignatureField.FieldPermissions.e_include, fields_to_lock); #if USE_DOTNET_CRYPTO DotNetCryptoSignatureHandler sigHandler = new DotNetCryptoSignatureHandler(in_private_key_file_path, in_keyfile_password); SDF.SignatureHandlerId sigHandlerId = doc.AddSignatureHandler(sigHandler); found_approval_signature_digsig_field.CertifyOnNextSaveWithCustomHandler(sigHandlerId); #else certification_sig_field.CertifyOnNextSave(in_private_key_file_path, in_keyfile_password); #endif ///// (OPTIONAL) Add more information to the signature dictionary. certification_sig_field.SetLocation("Vancouver, BC"); certification_sig_field.SetReason("Document certification."); certification_sig_field.SetContactInfo("www.pdftron.com"); ///// End of optional sig info code. // Save the PDFDoc. Once the method below is called, PDFNetC will also sign the document using the information provided. doc.Save(in_outpath, 0); } Console.Out.WriteLine("================================================================================"); }
private static void GrepDates(String pdf_file) { Console.WriteLine("Search dates in " + pdf_file); string text_name = pdf_file.Replace(".pdf", ".txt"); string input_file = pdf_file; ErrorCode error_code = Library.Initialize(sn, key); if (error_code != ErrorCode.e_ErrSuccess) { Console.WriteLine("Library Initialize Error: {0}", error_code); return; } var text = ""; try { using (var doc = new PDFDoc(input_file)) { error_code = doc.Load(null); if (error_code != ErrorCode.e_ErrSuccess) { Console.WriteLine("The PDFDoc " + input_file + " Error: " + error_code); return; } using (var writer = new StreamWriter(text_name, false, System.Text.Encoding.Unicode)) { int pageCount = doc.GetPageCount(); for (int i = 0; i < pageCount; i++) { using (var page = doc.GetPage(i)) { page.StartParse((int)PDFPage.ParseFlags.e_ParsePageNormal, null, false); // Get the text select object. using (var text_select = new TextPage(page, (int)TextPage.TextParseFlags.e_ParseTextNormal)) { int count = text_select.GetCharCount(); if (count > 0) { String chars = text_select.GetChars(0, count); text = text + chars; } } } } foreach (var dateTime in GetDates(text)) { writer.WriteLine(dateTime.Date.ToString("yyyy-MM-dd")); } } } } catch (System.Exception e) { Console.WriteLine(e.Message); } Library.Release(); }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. // string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; // The vector used to store the name and count of all fields. // This is used later on to clone the fields Dictionary <string, int> field_names = new Dictionary <string, int>(); //---------------------------------------------------------------------------------- // Example 1: Programatically create new Form Fields and Widget Annotations. //---------------------------------------------------------------------------------- try { using (PDFDoc doc = new PDFDoc()) { // Create a blank new page and add some form fields. Page blank_page = doc.PageCreate(); // Text Widget Creation // Create an empty text widget with black text. TextWidget text1 = TextWidget.Create(doc, new Rect(110, 700, 380, 730)); text1.SetText("Basic Text Field"); text1.RefreshAppearance(); blank_page.AnnotPushBack(text1); // Create a vertical text widget with blue text and a yellow background. TextWidget text2 = TextWidget.Create(doc, new Rect(50, 400, 90, 730)); text2.SetRotation(90); // Set the text content. text2.SetText(" ****Lucky Stars!****"); // Set the font type, text color, font size, border color and background color. text2.SetFont(Font.Create(doc, Font.StandardType1Font.e_helvetica_oblique)); text2.SetFontSize(28); text2.SetTextColor(new ColorPt(0, 0, 1), 3); text2.SetBorderColor(new ColorPt(0, 0, 0), 3); text2.SetBackgroundColor(new ColorPt(1, 1, 0), 3); text2.RefreshAppearance(); // Add the annotation to the page. blank_page.AnnotPushBack(text2); // Create two new text widget with Field names employee.name.first and employee.name.last // This logic shows how these widgets can be created using either a field name string or // a Field object TextWidget text3 = TextWidget.Create(doc, new Rect(110, 660, 380, 690), "employee.name.first"); text3.SetText("Levi"); text3.SetFont(Font.Create(doc, Font.StandardType1Font.e_times_bold)); text3.RefreshAppearance(); blank_page.AnnotPushBack(text3); Field emp_last_name = doc.FieldCreate("employee.name.last", Field.Type.e_text, "Ackerman"); TextWidget text4 = TextWidget.Create(doc, new Rect(110, 620, 380, 650), emp_last_name); text4.SetFont(Font.Create(doc, Font.StandardType1Font.e_times_bold)); text4.RefreshAppearance(); blank_page.AnnotPushBack(text4); // Signature Widget Creation (unsigned) SignatureWidget signature1 = SignatureWidget.Create(doc, new Rect(110, 560, 260, 610)); signature1.RefreshAppearance(); blank_page.AnnotPushBack(signature1); // CheckBox Widget Creation // Create a check box widget that is not checked. CheckBoxWidget check1 = CheckBoxWidget.Create(doc, new Rect(140, 490, 170, 520)); check1.RefreshAppearance(); blank_page.AnnotPushBack(check1); // Create a check box widget that is checked. CheckBoxWidget check2 = CheckBoxWidget.Create(doc, new Rect(190, 490, 250, 540), "employee.name.check1"); check2.SetBackgroundColor(new ColorPt(1, 1, 1), 3); check2.SetBorderColor(new ColorPt(0, 0, 0), 3); // Check the widget (by default it is unchecked). check2.SetChecked(true); check2.RefreshAppearance(); blank_page.AnnotPushBack(check2); // PushButton Widget Creation PushButtonWidget pushbutton1 = PushButtonWidget.Create(doc, new Rect(380, 490, 520, 540)); pushbutton1.SetTextColor(new ColorPt(1, 1, 1), 3); pushbutton1.SetFontSize(36); pushbutton1.SetBackgroundColor(new ColorPt(0, 0, 0), 3); // Add a caption for the pushbutton. pushbutton1.SetStaticCaptionText("PushButton"); pushbutton1.RefreshAppearance(); blank_page.AnnotPushBack(pushbutton1); // ComboBox Widget Creation ComboBoxWidget combo1 = ComboBoxWidget.Create(doc, new Rect(280, 560, 580, 610)); // Add options to the combobox widget. combo1.AddOption("Combo Box No.1"); combo1.AddOption("Combo Box No.2"); combo1.AddOption("Combo Box No.3"); // Make one of the options in the combo box selected by default. combo1.SetSelectedOption("Combo Box No.2"); combo1.SetTextColor(new ColorPt(1, 0, 0), 3); combo1.SetFontSize(28); combo1.RefreshAppearance(); blank_page.AnnotPushBack(combo1); // ListBox Widget Creation ListBoxWidget list1 = ListBoxWidget.Create(doc, new Rect(400, 620, 580, 730)); // Add one option to the listbox widget. list1.AddOption("List Box No.1"); // Add multiple options to the listbox widget in a batch. string[] list_options = new string[2] { "List Box No.2", "List Box No.3" }; list1.AddOptions(list_options); // Select some of the options in list box as default options list1.SetSelectedOptions(list_options); // Enable list box to have multi-select when editing. list1.GetField().SetFlag(Field.Flag.e_multiselect, true); list1.SetFont(Font.Create(doc, Font.StandardType1Font.e_times_italic)); list1.SetTextColor(new ColorPt(1, 0, 0), 3); list1.SetFontSize(28); list1.SetBackgroundColor(new ColorPt(1, 1, 1), 3); list1.RefreshAppearance(); blank_page.AnnotPushBack(list1); // RadioButton Widget Creation // Create a radio button group and add three radio buttons in it. RadioButtonGroup radio_group = RadioButtonGroup.Create(doc, "RadioGroup"); RadioButtonWidget radiobutton1 = radio_group.Add(new Rect(140, 410, 190, 460)); radiobutton1.SetBackgroundColor(new ColorPt(1, 1, 0), 3); radiobutton1.RefreshAppearance(); RadioButtonWidget radiobutton2 = radio_group.Add(new Rect(310, 410, 360, 460)); radiobutton2.SetBackgroundColor(new ColorPt(0, 1, 0), 3); radiobutton2.RefreshAppearance(); RadioButtonWidget radiobutton3 = radio_group.Add(new Rect(480, 410, 530, 460)); // Enable the third radio button. By default the first one is selected radiobutton3.EnableButton(); radiobutton3.SetBackgroundColor(new ColorPt(0, 1, 1), 3); radiobutton3.RefreshAppearance(); radio_group.AddGroupButtonsToPage(blank_page); // Custom push button annotation creation PushButtonWidget custom_pushbutton1 = PushButtonWidget.Create(doc, new Rect(260, 320, 360, 360)); // Set the annotation appearance. custom_pushbutton1.SetAppearance(CreateCustomButtonAppearance(doc, false), Annot.AnnotationState.e_normal); // Create 'SubmitForm' action. The action will be linked to the button. FileSpec url = FileSpec.CreateURL(doc, "http://www.pdftron.com"); pdftron.PDF.Action button_action = pdftron.PDF.Action.CreateSubmitForm(url); // Associate the above action with 'Down' event in annotations action dictionary. Obj annot_action = custom_pushbutton1.GetSDFObj().PutDict("AA"); annot_action.Put("D", button_action.GetSDFObj()); blank_page.AnnotPushBack(custom_pushbutton1); // Add the page as the last page in the document. doc.PagePushBack(blank_page); // If you are not satisfied with the look of default auto-generated appearance // streams you can delete "AP" entry from the Widget annotation and set // "NeedAppearances" flag in AcroForm dictionary: // doc.GetAcroForm().PutBool("NeedAppearances", true); // This will force the viewer application to auto-generate new appearance streams // every time the document is opened. // // Alternatively you can generate custom annotation appearance using ElementWriter // and then set the "AP" entry in the widget dictionary to the new appearance // stream. // // Yet another option is to pre-populate field entries with dummy text. When // you edit the field values using PDFNet the new field appearances will match // the old ones. doc.RefreshFieldAppearances(); doc.Save(output_path + "forms_test1.pdf", 0); Console.WriteLine("Done."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //---------------------------------------------------------------------------------- // Example 2: // Fill-in forms / Modify values of existing fields. // Traverse all form fields in the document (and print out their names). // Search for specific fields in the document. //---------------------------------------------------------------------------------- try { using (PDFDoc doc = new PDFDoc(output_path + "forms_test1.pdf")) { doc.InitSecurityHandler(); FieldIterator itr; for (itr = doc.GetFieldIterator(); itr.HasNext(); itr.Next()) { Field field = itr.Current(); string cur_field_name = field.GetName(); // Add one to the count for this field name for later processing field_names[cur_field_name] = (field_names.ContainsKey(cur_field_name) ? field_names[cur_field_name] + 1 : 1); Console.WriteLine("Field name: {0}", field.GetName()); Console.WriteLine("Field partial name: {0}", field.GetPartialName()); string str_val = field.GetValueAsString(); Console.Write("Field type: "); Field.Type type = field.GetType(); switch (type) { case Field.Type.e_button: Console.WriteLine("Button"); break; case Field.Type.e_radio: Console.WriteLine("Radio button: Value = " + str_val); break; case Field.Type.e_check: field.SetValue(true); Console.WriteLine("Check box: Value = " + str_val); break; case Field.Type.e_text: { Console.WriteLine("Text"); // Edit all variable text in the document String old_value = "none"; if (field.GetValue() != null) { old_value = field.GetValue().GetAsPDFText(); } field.SetValue("This is a new value. The old one was: " + old_value); } break; case Field.Type.e_choice: Console.WriteLine("Choice"); break; case Field.Type.e_signature: Console.WriteLine("Signature"); break; } Console.WriteLine("------------------------------"); } // Search for a specific field Field fld = doc.GetField("employee.name.first"); if (fld != null) { Console.WriteLine("Field search for {0} was successful", fld.GetName()); } else { Console.WriteLine("Field search failed."); } // Regenerate field appearances. doc.RefreshFieldAppearances(); doc.Save(output_path + "forms_test_edit.pdf", 0); Console.WriteLine("Done."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //---------------------------------------------------------------------------------- // Sample: Form templating // Replicate pages and form data within a document. Then rename field names to make // them unique. //---------------------------------------------------------------------------------- try { // Sample: Copying the page with forms within the same document using (PDFDoc doc = new PDFDoc(output_path + "forms_test1.pdf")) { doc.InitSecurityHandler(); Page src_page = doc.GetPage(1); doc.PagePushBack(src_page); // Append several copies of the second page doc.PagePushBack(src_page); // Note that forms are successfully copied doc.PagePushBack(src_page); doc.PagePushBack(src_page); // Now we rename fields in order to make every field unique. // You can use this technique for dynamic template filling where you have a 'master' // form page that should be replicated, but with unique field names on every page. foreach (KeyValuePair <string, int> cur_field in field_names) { RenameAllFields(doc, cur_field.Key, cur_field.Value); } doc.Save(output_path + "forms_test1_cloned.pdf", 0); Console.WriteLine("Done."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } //---------------------------------------------------------------------------------- // Sample: // Flatten all form fields in a document. // Note that this sample is intended to show that it is possible to flatten // individual fields. PDFNet provides a utility function PDFDoc.FlattenAnnotations() // that will automatically flatten all fields. //---------------------------------------------------------------------------------- try { using (PDFDoc doc = new PDFDoc(output_path + "forms_test1.pdf")) { doc.InitSecurityHandler(); bool auto = true; if (auto) { doc.FlattenAnnotations(); } else // Manual flattening { // Traverse all pages PageIterator pitr = doc.GetPageIterator(); for (; pitr.HasNext(); pitr.Next()) { Page page = pitr.Current(); for (int i = page.GetNumAnnots() - 1; i >= 0; --i) { Annot annot = page.GetAnnot(i); if (annot.GetType() == Annot.Type.e_Widget) { annot.Flatten(page); } } } } doc.Save(output_path + "forms_test1_flattened.pdf", 0); Console.WriteLine("Done."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
public SortedDictionary <double, FormLineList>[] GetFormLines(int pageNum, LinePos startPos, LinePos endPos , bool isSubsequentPage, SortedDictionary <double, FormLineList> lastPageVerticalLines) { //Get the information of lines and extreme points by travelsaling the page horizontalLines = new SortedDictionary <double, FormLineList>(); verticalLines = new SortedDictionary <double, FormLineList>(); _pageNum = pageNum; _startPos = startPos; _endPos = endPos; Page page = _pdfDoc.GetPage(pageNum); pageSize = PdfTronHelper.GetPageSize(page); topBound.AxisValue = startPos == null ? pageSize[1] : startPos.AxisValue; bottomBound.AxisValue = endPos == null ? 0 : endPos.AxisValue; ii = 0; using (ElementReader page_reader = new ElementReader()) { page_reader.Begin(page); ProcessElements(page_reader); } RemoveLittleLines(horizontalLines); RemoveLittleLines(verticalLines); //Remove short lines. Rect posRect = GetTablePosRect(_pageNum, _startPos, _endPos, isSubsequentPage); RemoveTooShortAndTooLongLines(page, horizontalLines, true, posRect); bool isNotNeedGenerateVerLines = isSubsequentPage && lastPageVerticalLines != null && verticalLines.Count == lastPageVerticalLines.Count; if (!isNotNeedGenerateVerLines) { RemoveTooShortAndTooLongLines(page, verticalLines, false, posRect); } //Generate drawed lines. Rect areaRect; bool existRealRect = horizontalLines.Count > 1 && verticalLines.Count > 1 && IsRect(posRect, horizontalLines, verticalLines); areaRect = existRealRect ? GenerateRectByLines() : posRect; if (existRealRect || isSubsequentPage) { RemoveLinesNotInRect(areaRect, horizontalLines, verticalLines); isNotNeedGenerateVerLines = isSubsequentPage && lastPageVerticalLines != null && verticalLines.Count == lastPageVerticalLines.Count; } else { posRect = GetTablePosRect(_pageNum, _startPos, _endPos, true); } FormLineGenerator.RemoveSpareNearLines(verticalLines, false); if (!isNotNeedGenerateVerLines) { isNotNeedGenerateVerLines = isSubsequentPage && lastPageVerticalLines != null && verticalLines.Count == lastPageVerticalLines.Count; } FormLineGenerator lineGenerator = new FormLineGenerator(page, areaRect); SortedDictionary <double, FormLineList>[] lines = lineGenerator.GetFormLines(existRealRect, horizontalLines, verticalLines, isNotNeedGenerateVerLines); return(lines); }
static void AnnotationLowLevelAPI(PDFDoc doc) { Page page = doc.GetPage(1); Obj annots = page.GetAnnots(); if (annots == null) { // If there are no annotations, create a new annotation // array for the page. annots = doc.CreateIndirectArray(); page.GetSDFObj().Put("Annots", annots); } // Create the Text annotation Obj text_annot = doc.CreateIndirectDict(); text_annot.PutName("Subtype", "Text"); text_annot.PutBool("Open", true); text_annot.PutString("Contents", "The quick brown fox ate the lazy mouse."); text_annot.PutRect("Rect", 266, 116, 430, 204); // Insert the annotation in the page annotation array annots.PushBack(text_annot); // Create a Link annotation Obj link1 = doc.CreateIndirectDict(); link1.PutName("Subtype", "Link"); Destination dest = Destination.CreateFit(doc.GetPage(2)); link1.Put("Dest", dest.GetSDFObj()); link1.PutRect("Rect", 85, 705, 503, 661); annots.PushBack(link1); // Create another Link annotation Obj link2 = doc.CreateIndirectDict(); link2.PutName("Subtype", "Link"); Destination dest2 = Destination.CreateFit(doc.GetPage(3)); link2.Put("Dest", dest2.GetSDFObj()); link2.PutRect("Rect", 85, 638, 503, 594); annots.PushBack(link2); // Note that PDFNet APi can be used to modify existing annotations. // In the following example we will modify the second link annotation // (link2) so that it points to the 10th page. We also use a different // destination page fit type. link2.Put("Dest", Destination.CreateXYZ(doc.GetPage(10), 100, 792 - 70, 10).GetSDFObj()); // Create a third link annotation with a hyperlink action (all other // annotation types can be created in a similar way) Obj link3 = doc.CreateIndirectDict(); link3.PutName("Subtype", "Link"); link3.PutRect("Rect", 85, 570, 503, 524); // Create a URI action Obj action = link3.PutDict("A"); action.PutName("S", "URI"); action.PutString("URI", "http://www.pdftron.com"); annots.PushBack(link3); }
public static Annot CreatePDFAnnotation(BaseAnnotation annotation, PDFDoc pdfDocument, bool fromViewer) { _currentDoc = pdfDocument; Annot pdfAnnotation; switch (annotation.Properties.PropertyName) { case XMLHighlightText.Names.Highlight: case XMLHighlightArea.Names.Highlight: pdfAnnotation = setHighlight(annotation); break; case StickyNote.Names.StickyNote: pdfAnnotation = setStickyNote((StickyNote)annotation, fromViewer); break; case MarkArea.Names.MarkArea: pdfAnnotation = setMarkArea((MarkArea)annotation); break; case FreeText.Names.FreeText: pdfAnnotation = setFreeText((FreeText)annotation, fromViewer); break; case Circle.Names.Circle: pdfAnnotation = setCircle((Circle)annotation); break; case Square.Names.Square: pdfAnnotation = setSquare((Square)annotation); break; case Line.Names.Line: pdfAnnotation = setLine((Line)annotation); break; case StamperImage.Names.Stamper: pdfAnnotation = setStamperImage((StamperImage)annotation); break; case StamperText.Names.Stamper: pdfAnnotation = setStamperText((StamperText)annotation); break; case RubberStamp.Names.RubberStamp: pdfAnnotation = setRubberStamp((RubberStamp)annotation); break; case XMLSquiggly.Names.Squiggly: pdfAnnotation = setSquiggly((XMLSquiggly)annotation); break; case XMLStrikeout.Names.Strikeout: pdfAnnotation = setStrikeout((XMLStrikeout)annotation); break; case XMLUnderline.Names.Underline: pdfAnnotation = setUnderline((XMLUnderline)annotation); break; default: pdfAnnotation = Annot.Create(null, Annot.Type.e_3D, null); // For Compiler. Should never execute this break; } if (!(annotation is FreeText)) // Quick Fix, need to revamp it { pdfAnnotation.SetColor(AnnotationsMannager.ConvertColor(new double[] { annotation.ColorRed(), annotation.ColorGreen(), annotation.ColorBlue() }), 3); } _currentDoc.GetPage(annotation.Page()).AnnotPushBack(pdfAnnotation); return(pdfAnnotation); }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; // Sample code showing how to use high-level text extraction APIs. try { using (PDFDoc doc = new PDFDoc(input_path + "credit card numbers.pdf")) { doc.InitSecurityHandler(); Int32 page_num = 0; String result_str = "", ambient_string = ""; Highlights hlts = new Highlights(); TextSearch txt_search = new TextSearch(); Int32 mode = (Int32)(TextSearch.SearchMode.e_whole_word | TextSearch.SearchMode.e_page_stop | TextSearch.SearchMode.e_highlight); String pattern = "joHn sMiTh"; //call Begin() method to initialize the text search. txt_search.Begin(doc, pattern, mode, -1, -1); int step = 0; //call Run() method iteratively to find all matching instances. while (true) { TextSearch.ResultCode code = txt_search.Run(ref page_num, ref result_str, ref ambient_string, hlts); if (code == TextSearch.ResultCode.e_found) { if (step == 0) { //step 0: found "John Smith" //note that, here, 'ambient_string' and 'hlts' are not written to, //as 'e_ambient_string' and 'e_highlight' are not set. Console.WriteLine(result_str + "'s credit card number is: "); //now switch to using regular expressions to find John's credit card number mode = txt_search.GetMode(); mode |= (Int32)(TextSearch.SearchMode.e_reg_expression | TextSearch.SearchMode.e_highlight); txt_search.SetMode(mode); pattern = "\\d{4}-\\d{4}-\\d{4}-\\d{4}"; //or "(\\d{4}-){3}\\d{4}" txt_search.SetPattern(pattern); ++step; } else if (step == 1) { //step 1: found John's credit card number //result_str.ConvertToAscii(char_buf, 32, true); //cout << " " << char_buf << endl; Console.WriteLine(" " + result_str); //note that, here, 'hlts' is written to, as 'e_highlight' has been set. //output the highlight info of the credit card number hlts.Begin(doc); while (hlts.HasNext()) { Console.WriteLine("The current highlight is from page: " + hlts.GetCurrentPageNumber()); hlts.Next(); } //see if there is an AMEX card number pattern = "\\d{4}-\\d{6}-\\d{5}"; txt_search.SetPattern(pattern); ++step; } else if (step == 2) { //found an AMEX card number Console.WriteLine("\nThere is an AMEX card number:\n " + result_str); //change mode to find the owner of the credit card; supposedly, the owner's //name proceeds the number mode = txt_search.GetMode(); mode |= (Int32)(TextSearch.SearchMode.e_search_up); txt_search.SetMode(mode); pattern = "[A-z]++ [A-z]++"; txt_search.SetPattern(pattern); ++step; } else if (step == 3) { //found the owner's name of the AMEX card Console.WriteLine("Is the owner's name:\n " + result_str + "?"); //add a link annotation based on the location of the found instance hlts.Begin(doc); while (hlts.HasNext()) { Page cur_page = doc.GetPage(hlts.GetCurrentPageNumber()); double[] quads = hlts.GetCurrentQuads(); int quad_count = quads.Length / 8; for (int i = 0; i < quad_count; ++i) { //assume each quad is an axis-aligned rectangle int offset = 8 * i; double x1 = Math.Min(Math.Min(Math.Min(quads[offset + 0], quads[offset + 2]), quads[offset + 4]), quads[offset + 6]); double x2 = Math.Max(Math.Max(Math.Max(quads[offset + 0], quads[offset + 2]), quads[offset + 4]), quads[offset + 6]); double y1 = Math.Min(Math.Min(Math.Min(quads[offset + 1], quads[offset + 3]), quads[offset + 5]), quads[offset + 7]); double y2 = Math.Max(Math.Max(Math.Max(quads[offset + 1], quads[offset + 3]), quads[offset + 5]), quads[offset + 7]); pdftron.PDF.Annots.Link hyper_link = pdftron.PDF.Annots.Link.Create(doc, new Rect(x1, y1, x2, y2), pdftron.PDF.Action.CreateURI(doc, "http://www.pdftron.com")); hyper_link.RefreshAppearance(); cur_page.AnnotPushBack(hyper_link); } hlts.Next(); } string output_path = "../../TestFiles/Output/"; doc.Save(output_path + "credit card numbers_linked.pdf", SDFDoc.SaveOptions.e_linearized); break; } } else if (code == TextSearch.ResultCode.e_page) { //you can update your UI here, if needed } else { break; } } } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; try { // Read a PDF document from a stream or pass-in a memory buffer... FileStream istm = new FileStream(input_path + "tiger.pdf", FileMode.Open, FileAccess.Read); using (PDFDoc doc = new PDFDoc(istm)) using (ElementWriter writer = new ElementWriter()) using (ElementReader reader = new ElementReader()) { doc.InitSecurityHandler(); int num_pages = doc.GetPageCount(); Element element; // Perform some document editing ... // Here we simply copy all elements from one page to another. for (int i = 1; i <= num_pages; ++i) { Page pg = doc.GetPage(2 * i - 1); reader.Begin(pg); Page new_page = doc.PageCreate(pg.GetMediaBox()); doc.PageInsert(doc.GetPageIterator(2 * i), new_page); writer.Begin(new_page); while ((element = reader.Next()) != null) // Read page contents { writer.WriteElement(element); } writer.End(); reader.End(); } doc.Save(output_path + "doc_memory_edit.pdf", SDFDoc.SaveOptions.e_remove_unused); // Save the document to a stream or a memory buffer... using (FileStream ostm = new FileStream(output_path + "doc_memory_edit.txt", FileMode.Create, FileAccess.Write)) { doc.Save(ostm, SDFDoc.SaveOptions.e_remove_unused); } // Read some data from the file stored in memory reader.Begin(doc.GetPage(1)); while ((element = reader.Next()) != null) { if (element.GetType() == Element.Type.e_path) { Console.Write("Path, "); } } reader.End(); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("Done. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ..."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
/// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; // The following example illustrates how to create and edit the outline tree // using high-level Bookmark methods. try { using (PDFDoc doc = new PDFDoc(input_path + "numbered.pdf")) { doc.InitSecurityHandler(); // Lets first create the root bookmark items. Bookmark red = Bookmark.Create(doc, "Red"); Bookmark green = Bookmark.Create(doc, "Green"); Bookmark blue = Bookmark.Create(doc, "Blue"); doc.AddRootBookmark(red); doc.AddRootBookmark(green); doc.AddRootBookmark(blue); // You can also add new root bookmarks using Bookmark.AddNext("...") blue.AddNext("foo"); blue.AddNext("bar"); // We can now associate new bookmarks with page destinations: // The following example creates an 'explicit' destination (see // section '8.2.1 Destinations' in PDF Reference for more details) Destination red_dest = Destination.CreateFit(doc.GetPage(1)); red.SetAction(pdftron.PDF.Action.CreateGoto(red_dest)); // Create an explicit destination to the first green page in the document green.SetAction(pdftron.PDF.Action.CreateGoto( Destination.CreateFit(doc.GetPage(10)))); // The following example creates a 'named' destination (see // section '8.2.1 Destinations' in PDF Reference for more details) // Named destinations have certain advantages over explicit destinations. String key = "blue1"; pdftron.PDF.Action blue_action = pdftron.PDF.Action.CreateGoto(key, Destination.CreateFit(doc.GetPage(19))); blue.SetAction(blue_action); // We can now add children Bookmarks Bookmark sub_red1 = red.AddChild("Red - Page 1"); sub_red1.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(1)))); Bookmark sub_red2 = red.AddChild("Red - Page 2"); sub_red2.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(2)))); Bookmark sub_red3 = red.AddChild("Red - Page 3"); sub_red3.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(3)))); Bookmark sub_red4 = sub_red3.AddChild("Red - Page 4"); sub_red4.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(4)))); Bookmark sub_red5 = sub_red3.AddChild("Red - Page 5"); sub_red5.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(5)))); Bookmark sub_red6 = sub_red3.AddChild("Red - Page 6"); sub_red6.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(6)))); // Example of how to find and delete a bookmark by title text. Bookmark foo = doc.GetFirstBookmark().Find("foo"); if (foo.IsValid()) { foo.Delete(); } Bookmark bar = doc.GetFirstBookmark().Find("bar"); if (bar.IsValid()) { bar.Delete(); } // Adding color to Bookmarks. Color and other formatting can help readers // get around more easily in large PDF documents. red.SetColor(1, 0, 0); green.SetColor(0, 1, 0); green.SetFlags(2); // set bold font blue.SetColor(0, 0, 1); blue.SetFlags(3); // set bold and italic doc.Save(output_path + "bookmark.pdf", 0); Console.WriteLine("Done. Result saved in bookmark.pdf"); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } // The following example illustrates how to traverse the outline tree using // Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(), // Bookmark.GetFirstChild () and Bookmark.GetLastChild (). try { // Open the document that was saved in the previous code sample using (PDFDoc doc = new PDFDoc(output_path + "bookmark.pdf")) { doc.InitSecurityHandler(); Bookmark root = doc.GetFirstBookmark(); PrintOutlineTree(root); Console.WriteLine("Done."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } // The following example illustrates how to create a Bookmark to a page // in a remote document. A remote go-to action is similar to an ordinary // go-to action, but jumps to a destination in another PDF file instead // of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF // Reference Manual for details. try { using (PDFDoc doc = new PDFDoc(output_path + "bookmark.pdf")) { doc.InitSecurityHandler(); // Create file specification (the file referred to by the remote bookmark) Obj file_spec = doc.CreateIndirectDict(); file_spec.PutName("Type", "Filespec"); file_spec.PutString("F", "bookmark.pdf"); FileSpec spec = new FileSpec(file_spec); pdftron.PDF.Action goto_remote = pdftron.PDF.Action.CreateGotoRemote(spec, 5, true); Bookmark remoteBookmark1 = Bookmark.Create(doc, "REMOTE BOOKMARK 1"); remoteBookmark1.SetAction(goto_remote); doc.AddRootBookmark(remoteBookmark1); // Create another remote bookmark, but this time using the low-level SDF/Cos API. Bookmark remoteBookmark2 = Bookmark.Create(doc, "REMOTE BOOKMARK 2"); doc.AddRootBookmark(remoteBookmark2); Obj gotoR = remoteBookmark2.GetSDFObj().PutDict("A"); { // Create the 'Action' dictionary. gotoR.PutName("S", "GoToR"); // Set action type gotoR.PutBool("NewWindow", true); // Set the file specification gotoR.Put("F", file_spec); // Set the destination. Obj dest = gotoR.PutArray("D"); dest.PushBackNumber(9); // jump to the tenth page. Note that Acrobat indexes pages from 0. dest.PushBackName("Fit"); // Fit the page } doc.Save(output_path + "bookmark_remote.pdf", SDFDoc.SaveOptions.e_linearized); Console.WriteLine("Done. Result saved in bookmark_remote.pdf"); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }