/** * Split PDF string into array of single character PDF strings. * @param string PDF string to be splitted. * @return splitted PDF string. */ private PdfString[] SplitString(PdfString @string) { List <PdfString> strings = new List <PdfString>(); String stringValue = @string.ToString(); for (int i = 0; i < stringValue.Length; i++) { PdfString newString = new PdfString(stringValue.Substring(i, 1), @string.Encoding); String text = DecodeSingleCharacter(newString); if (text.Length == 0 && i < stringValue.Length - 1) { newString = new PdfString(stringValue.Substring(i, 2), @string.Encoding); i++; } strings.Add(newString); } return(strings.ToArray()); }
/// <summary> /// Parses a stream object and removes OCGs. </summary> /// <param name="stream"> a stream object </param> /// <param name="resources"> the resources dictionary of that object (containing info about the OCGs) </param> public virtual void Parse(PRStream stream, PdfDictionary resources) { baos = new MemoryStream(); properties = resources.GetAsDict(PdfName.PROPERTIES); xobj = new HashSet2 <PdfName>(); PdfDictionary xobjects = resources.GetAsDict(PdfName.XOBJECT); if (xobjects != null) { // remove XObject (form or image) that belong to an OCG that needs to be removed foreach (PdfName name in xobjects.Keys) { PRStream xobject = (PRStream)xobjects.GetAsStream(name); PdfDictionary oc = xobject.GetAsDict(PdfName.OC); if (oc != null) { PdfString ocname = oc.GetAsString(PdfName.NAME); if (ocname != null && ocgs.Contains(ocname.ToString())) { xobj.Add(name); } } } foreach (PdfName name in xobj) { xobjects.Remove(name); } } // parse the content stream byte[] contentBytes = PdfReader.GetStreamBytes(stream); PRTokeniser tokeniser = new PRTokeniser(new RandomAccessFileOrArray(contentBytes)); PdfContentParser ps = new PdfContentParser(tokeniser); List <PdfObject> operands = new List <PdfObject>(); while (ps.Parse(operands).Count > 0) { PdfLiteral @operator = (PdfLiteral)operands[operands.Count - 1]; ProcessOperator(this, @operator, operands); } baos.Flush(); baos.Close(); stream.SetData(baos.GetBuffer()); }
/// <summary> /// Keeps track of the MarkedContent state. </summary> /// <param name="ocref"> a reference to an OCG dictionary </param> protected internal virtual void CheckMarkedContentStart(PdfName ocref) { if (mc_balance > 0) { mc_balance++; return; } PdfDictionary ocdict = properties.GetAsDict(ocref); if (ocdict == null) { return; } PdfString ocname = ocdict.GetAsString(PdfName.NAME); if (ocname == null) { return; } if (ocgs.Contains(ocname.ToString())) { mc_balance++; } }
/* Parse PDf file annotations */ static void parseAnnotations(PdfReader reader, List <cMark> markers) { markers.Clear(); // on each page for (int pg = 1; pg < reader.NumberOfPages + 1; pg++) { PdfDictionary pagedic = reader.GetPageN(pg); // get annotations array PdfArray annotarray = (PdfArray)PdfReader.GetPdfObject(pagedic.Get(PdfName.ANNOTS)); // if no annotation ... if (annotarray == null || annotarray.Size == 0) { continue; } // on each annotation reference... foreach (PdfIndirectReference annot in annotarray.ArrayList) { PdfDictionary annotationDic = (PdfDictionary)PdfReader.GetPdfObject(annot); PdfName subType = (PdfName)annotationDic.Get(PdfName.SUBTYPE); PdfString contents = annotationDic.GetAsString(PdfName.CONTENTS); // if simple text... if ((contents != null) && ((subType.Equals(PdfName.TEXT)) || (subType.Equals(PdfName.FREETEXT)) ) ) { String value = contents.ToString(); // single marker element cMark mrk = new cMark(cMark.TypeMarker.TypeAnnotation, cMark.TypeAnnotationSubType.TypeAnnotation_NONE); mrk.pageNum = pg; mrk.title = value; if (annotationDic.Get(PdfName.RECT) != null) { PdfArray coord = annotationDic.GetAsArray(PdfName.RECT); PdfRectangle textRect = new PdfRectangle( ((PdfNumber)coord[0]).FloatValue, ((PdfNumber)coord[1]).FloatValue, ((PdfNumber)coord[2]).FloatValue, ((PdfNumber)coord[3]).FloatValue); mrk.annotRect = textRect.Rectangle; } markers.Add(mrk); } // if decorated text... if ((subType.Equals(PdfName.UNDERLINE)) || (subType.Equals(PdfName.HIGHLIGHT)) || (subType.Equals(PdfName.STRIKEOUT)) || (subType.Equals(PdfName.SQUIGGLY))) { cMark mrk = new cMark(cMark.TypeMarker.TypeAnnotation, cMark.TypeAnnotationSubType.TypeAnnotation_NONE); mrk.pageNum = pg; if (subType.Equals(PdfName.UNDERLINE)) { mrk.eAnnotationSubType = cMark.TypeAnnotationSubType.TypeAnnotation_UNDERLINE; } else if (subType.Equals(PdfName.HIGHLIGHT)) { mrk.eAnnotationSubType = cMark.TypeAnnotationSubType.TypeAnnotation_HIGHLIGHT; } else if (subType.Equals(PdfName.STRIKEOUT)) { mrk.eAnnotationSubType = cMark.TypeAnnotationSubType.TypeAnnotation_STRIKEOUT; } else if (subType.Equals(PdfName.SQUIGGLY)) { mrk.eAnnotationSubType = cMark.TypeAnnotationSubType.TypeAnnotation_SQUIGGLY; } PdfObject pdfObjectQuad = annotationDic.Get(PdfName.QUADPOINTS); if (pdfObjectQuad != null) { PdfArray rect = annotationDic.GetAsArray(PdfName.QUADPOINTS); // float llx, float lly, float urx, float ury float lowX = Math.Min(((PdfNumber)rect[0]).FloatValue, ((PdfNumber)rect[2]).FloatValue); lowX = Math.Min(lowX, ((PdfNumber)rect[4]).FloatValue); lowX = Math.Min(lowX, ((PdfNumber)rect[6]).FloatValue); float lowY = Math.Min(((PdfNumber)rect[1]).FloatValue, ((PdfNumber)rect[3]).FloatValue); lowY = Math.Min(lowY, ((PdfNumber)rect[5]).FloatValue); lowY = Math.Min(lowY, ((PdfNumber)rect[7]).FloatValue); float upX = Math.Max(((PdfNumber)rect[0]).FloatValue, ((PdfNumber)rect[2]).FloatValue); upX = Math.Max(upX, ((PdfNumber)rect[4]).FloatValue); upX = Math.Max(upX, ((PdfNumber)rect[6]).FloatValue); float upY = Math.Max(((PdfNumber)rect[1]).FloatValue, ((PdfNumber)rect[3]).FloatValue); upY = Math.Max(upY, ((PdfNumber)rect[5]).FloatValue); upY = Math.Max(upY, ((PdfNumber)rect[7]).FloatValue); PdfRectangle textRect = new PdfRectangle(lowX, lowY, upX, upY); RenderFilter[] filter = { new RegionTextRenderFilter(textRect.Rectangle) }; ITextExtractionStrategy strategy; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= reader.NumberOfPages; i++) { strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter); sb.AppendLine(PdfTextExtractor.GetTextFromPage(reader, i, strategy)); } String result = sb.ToString(); mrk.title = result; mrk.annotRect = textRect.Rectangle; markers.Add(mrk); } } } } }
/// <summary> /// Parses a stream object and removes OCGs. </summary> /// <param name="stream"> a stream object </param> /// <param name="resources"> the resources dictionary of that object (containing info about the OCGs) </param> public virtual void Parse(PRStream stream, PdfDictionary resources) { baos = new MemoryStream(); properties = resources.GetAsDict(PdfName.PROPERTIES); xobj = new HashSet2 <PdfName>(); PdfDictionary xobjects = resources.GetAsDict(PdfName.XOBJECT); if (xobjects != null) { // remove XObject (form or image) that belong to an OCG that needs to be removed foreach (PdfName name in xobjects.Keys) { PRStream xobject = (PRStream)xobjects.GetAsStream(name); PdfDictionary oc = xobject.GetAsDict(PdfName.OC); if (oc != null) { PdfString ocname = oc.GetAsString(PdfName.NAME); if (ocname != null && ocgs.Contains(ocname.ToString())) { xobj.Add(name); } } } foreach (PdfName name in xobj) { xobjects.Remove(name); } } // parse the content stream byte[] contentBytes = PdfReader.GetStreamBytes(stream); PRTokeniser tokeniser = new PRTokeniser(new RandomAccessFileOrArray(contentBytes)); PdfContentParser ps = new PdfContentParser(tokeniser); List <PdfObject> operands = new List <PdfObject>(); while (ps.Parse(operands).Count > 0) { PdfLiteral @operator = (PdfLiteral)operands[operands.Count - 1]; ProcessOperator(this, @operator, operands); if ("BI".Equals(@operator.ToString())) { int found = 0; int ch; bool immediateAfterBI = true; while ((ch = tokeniser.Read()) != -1) { if (!immediateAfterBI || !PRTokeniser.IsWhitespace(ch)) { baos.WriteByte((byte)ch); } immediateAfterBI = false; if (found == 0 && PRTokeniser.IsWhitespace(ch)) { found++; } else if (found == 1 && ch == 'E') { found++; } else if (found == 1 && PRTokeniser.IsWhitespace(ch)) { // this clause is needed if we have a white space character that is part of the image data // followed by a whitespace character that precedes the EI operator. In this case, we need // to flush the first whitespace, then treat the current whitespace as the first potential // character for the end of stream check. Note that we don't increment 'found' here. } else if (found == 2 && ch == 'I') { found++; } else if (found == 3 && PRTokeniser.IsWhitespace(ch)) { break; } else { found = 0; } } } } baos.Flush(); baos.Close(); stream.SetData(baos.GetBuffer()); }
/// <summary>Verifies a signature.</summary> /// <remarks> /// Verifies a signature. Further verification can be done on the returned /// <see cref="PdfPKCS7"/> /// object. /// </remarks> /// <param name="name">the signature field name</param> /// <param name="provider">the provider or null for the default provider</param> /// <returns>PdfPKCS7 object to continue the verification</returns> public virtual PdfPKCS7 VerifySignature(String name) { PdfDictionary v = GetSignatureDictionary(name); if (v == null) { return(null); } try { PdfName sub = v.GetAsName(PdfName.SubFilter); PdfString contents = v.GetAsString(PdfName.Contents); PdfPKCS7 pk = null; if (sub.Equals(PdfName.Adbe_x509_rsa_sha1)) { PdfString cert = v.GetAsString(PdfName.Cert); if (cert == null) { cert = v.GetAsArray(PdfName.Cert).GetAsString(0); } pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), cert.GetValueBytes()); } else { pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), sub); } UpdateByteRange(pk, v); PdfString str = v.GetAsString(PdfName.M); if (str != null) { pk.SetSignDate(PdfDate.Decode(str.ToString())); } PdfObject obj = v.Get(PdfName.Name); if (obj != null) { if (obj.IsString()) { pk.SetSignName(((PdfString)obj).ToUnicodeString()); } else { if (obj.IsName()) { pk.SetSignName(((PdfName)obj).GetValue()); } } } str = v.GetAsString(PdfName.Reason); if (str != null) { pk.SetReason(str.ToUnicodeString()); } str = v.GetAsString(PdfName.Location); if (str != null) { pk.SetLocation(str.ToUnicodeString()); } return(pk); } catch (Exception e) { throw new PdfException(e); } }
/** * Split PDF string into array of single character PDF strings. * @param string PDF string to be splitted. * @return splitted PDF string. */ private PdfString[] SplitString(PdfString @string) { List<PdfString> strings = new List<PdfString>(); String stringValue = @string.ToString(); for (int i = 0; i < stringValue.Length; i++) { PdfString newString = new PdfString(stringValue.Substring(i, 1), @string.Encoding); String text = DecodeSingleCharacter(newString); if (text.Length == 0 && i < stringValue.Length - 1) { newString = new PdfString(stringValue.Substring(i, 2), @string.Encoding); i++; } strings.Add(newString); } return strings.ToArray(); }
private void GetFormFields(Stream source) { PdfReader reader = null; try { reader = new PdfReader(source); PRAcroForm form = reader.AcroForm; if (form == null) { //ac.debugText("This document has no fields."); return; } //PdfLister list = new PdfLister(System.out); Hashtable refToField = new Hashtable(); ArrayList fields = form.Fields; foreach (PRAcroForm.FieldInformation field in fields) { refToField.Add(field.Ref.Number, field); } for (int page = 1; page <= reader.NumberOfPages; page++) { PdfDictionary dPage = reader.GetPageN(page); PdfArray annots = (PdfArray)PdfReader.GetPdfObject((PdfObject)dPage.Get(PdfName.ANNOTS)); if (annots == null) { break; } ArrayList ali = annots.ArrayList; PRIndirectReference iRef = null; foreach (PdfObject refObj in ali) { PdfDictionary an = (PdfDictionary)PdfReader.GetPdfObject(refObj); PdfName name = (PdfName)an.Get(PdfName.SUBTYPE); if (name == null || !name.Equals(PdfName.WIDGET)) { break; } PdfArray rect = (PdfArray)PdfReader.GetPdfObject(an.Get(PdfName.RECT)); string fName = ""; PRAcroForm.FieldInformation field = null; while ((an != null)) { PdfString tName = (PdfString)an.Get(PdfName.T); if ((tName != null)) { fName = tName.ToString() + "." + fName; } if (refObj.IsIndirect() && field == null) { iRef = (PRIndirectReference)refObj; field = (PRAcroForm.FieldInformation)refToField[iRef.Number]; } //refObj = (PdfObject)an.Get(PdfName.PARENT); an = (PdfDictionary)PdfReader.GetPdfObject((PdfObject)an.Get(PdfName.PARENT)); } if (fName.EndsWith(".")) { fName = fName.Substring(0, fName.Length - 1); } PDFFieldLocation tempLoc = new PDFFieldLocation(); ArrayList arr = rect.ArrayList; tempLoc.fieldName = fName; tempLoc.page = page; tempLoc.x1 = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[0])).FloatValue; tempLoc.y1 = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[1])).FloatValue; tempLoc.x2 = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[2])).FloatValue; tempLoc.y2 = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[3])).FloatValue; this.PFDlocs.Add(tempLoc); } } } catch (Exception e) { throw new Exception("Critical Exception in GetFormFields", e); } finally { if ((reader != null)) { reader.Close(); } } }
private SignedDocumentInfo CollectInfo(String documentPath) { SignedDocumentInfo docInfo = new SignedDocumentInfo(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(documentPath)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); SignatureUtil signUtil = new SignatureUtil(pdfDoc); IList <String> names = signUtil.GetSignatureNames(); docInfo.SetNumberOfTotalRevisions(signUtil.GetTotalRevisions()); SignaturePermissions perms = null; IList <SignatureInfo> signInfos = new List <SignatureInfo>(); foreach (String name in names) { SignatureInfo sigInfo = new SignatureInfo(); sigInfo.SetSignatureName(name); sigInfo.SetRevisionNumber(signUtil.GetRevision(name)); sigInfo.SetSignatureCoversWholeDocument(signUtil.SignatureCoversWholeDocument(name)); IList <PdfWidgetAnnotation> widgetAnnotationsList = form.GetField(name).GetWidgets(); if (widgetAnnotationsList != null && widgetAnnotationsList.Count > 0) { sigInfo.SetSignaturePosition(widgetAnnotationsList[0].GetRectangle().ToRectangle()); } PdfPKCS7 pkcs7 = signUtil.ReadSignatureData(name); sigInfo.SetDigestAlgorithm(pkcs7.GetHashAlgorithm()); sigInfo.SetEncryptionAlgorithm(pkcs7.GetEncryptionAlgorithm()); PdfName filterSubtype = pkcs7.GetFilterSubtype(); if (filterSubtype != null) { sigInfo.SetFilterSubtype(filterSubtype.ToString()); } X509Certificate signCert = pkcs7.GetSigningCertificate(); sigInfo.SetSignerName(iText.Signatures.CertificateInfo.GetSubjectFields(signCert).GetField("CN")); sigInfo.SetAlternativeSignerName(pkcs7.GetSignName()); sigInfo.SetSignDate(pkcs7.GetSignDate().ToUniversalTime()); if (TimestampConstants.UNDEFINED_TIMESTAMP_DATE != pkcs7.GetTimeStampDate()) { sigInfo.SetTimeStamp(pkcs7.GetTimeStampDate().ToUniversalTime()); TimeStampToken ts = pkcs7.GetTimeStampToken(); sigInfo.SetTimeStampService(ts.TimeStampInfo.Tsa.ToString()); } sigInfo.SetLocation(pkcs7.GetLocation()); sigInfo.SetReason(pkcs7.GetReason()); PdfDictionary sigDict = signUtil.GetSignatureDictionary(name); PdfString contactInfo = sigDict.GetAsString(PdfName.ContactInfo); if (contactInfo != null) { sigInfo.SetContactInfo(contactInfo.ToString()); } perms = new SignaturePermissions(sigDict, perms); sigInfo.SetIsCertifiaction(perms.IsCertification()); sigInfo.SetIsFieldsFillAllowed(perms.IsFillInAllowed()); sigInfo.SetIsAddingAnnotationsAllowed(perms.IsAnnotationsAllowed()); IList <String> fieldLocks = new List <String>(); foreach (SignaturePermissions.FieldLock Lock in perms.GetFieldLocks()) { fieldLocks.Add(Lock.ToString()); } sigInfo.SetFieldsLocks(fieldLocks); X509Certificate[] certs = pkcs7.GetSignCertificateChain(); IList <CertificateInfo> certInfos = new List <CertificateInfo>(); for (int i = 0; i < certs.Length; i++) { X509Certificate cert = (X509Certificate)certs[i]; CertificateInfo certInfo = new CertificateInfo(); certInfo.SetIssuer(cert.IssuerDN); certInfo.SetSubject(cert.SubjectDN); certInfo.SetValidFrom(cert.NotBefore); certInfo.SetValidTo(cert.NotAfter); certInfos.Add(certInfo); } sigInfo.SetCertificateInfos(certInfos); signInfos.Add(sigInfo); } docInfo.SetSignatureInfos(signInfos); return(docInfo); }
public Annotation(PdfDictionary annotation, Geometry geometry) { this.Geometry = geometry; PdfString createdString = annotation.GetAsString(PdfName.CREATIONDATE); this.Created = (createdString == null) ? DateTime.MinValue : createdString.ToString().ToDateTime(); PdfString updatedString = annotation.GetAsString(PdfName.M); this.Updated = (updatedString == null) ? DateTime.MinValue : updatedString.ToString().ToDateTime(); PdfString contents = annotation.GetAsString(PdfName.CONTENTS); this.Contents = (contents == null) ? string.Empty : contents.ToString(); PdfString author = annotation.GetAsString(PdfName.T); this.Author = (author == null) ? string.Empty : author.ToString(); PdfString subject = annotation.GetAsString(PdfName.SUBJECT); this.Subject = (subject == null) ? string.Empty : subject.ToString(); PdfArray color = annotation.GetAsArray(PdfName.C); if (color != null && color.Size == 3) { var pdfcolor = new iTextSharp.text.BaseColor(color[0].ToFloat(), color[1].ToFloat(), color[2].ToFloat()); this.Color = pdfcolor.ToDSColor(); } if (this.Color == null) { PdfString ds = annotation.GetAsString(PdfName.DS); //font: Helvetica 12pt; text-align:left; margin:3pt; line-height:13.8pt; color:#000000 if (ds != null) { if (ds.ToString().Contains(';')) { string[] data = ds.ToString().Split(';'); Dictionary <string, string> datadict = new Dictionary <string, string>(); foreach (string d in data) { if (d.Contains(':')) { string[] vp = d.Split(':'); string key = vp[0].Replace(" ", ""); string val = vp[1].Replace(" ", ""); if (!datadict.ContainsKey(key)) { datadict.Add(key, val); } } } if (datadict.ContainsKey("color")) { var syscolor = System.Drawing.ColorTranslator.FromHtml(datadict["color"].ToUpper()); if (syscolor != null) { this.Color = DSCore.Color.ByARGB(syscolor.A, syscolor.R, syscolor.G, syscolor.B); } } } } if (this.Color == null) { this.Color = DSCore.Color.ByARGB(255, 255, 0, 0); } } }
static void Main(string[] args) { //get user's desktop path for log file, and add log file name string logFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\containsLink_log.txt"; //retrive file names from user's specified directory and sub directories Console.WriteLine("Enter directory to search: "); string path = Console.ReadLine(); string[] myFiles = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); //process each file foreach (string filename in myFiles) { //skip file if just temp file if (filename.Substring(0, 1) != "~") { //write filename to log file System.IO.File.AppendAllText(logFilePath, filename); System.IO.File.AppendAllText(logFilePath, Environment.NewLine); Console.WriteLine(filename); //make list to store file's links List <string> links = new List <string>(); //find file's extension (type) int extLoc = filename.LastIndexOf("."); int endLoc = filename.Length - extLoc - 1; string fileExt = filename.Substring(extLoc + 1, endLoc); //process file according to its type, extracting the file's links if (fileExt == "pdf") { //Setup some variables to be used later PdfReader reader = default(PdfReader); int pageCount = 0; PdfDictionary pageDictionary = default(PdfDictionary); PdfArray annots = default(PdfArray); //Open our reader reader = new PdfReader(filename); //Get the page cont pageCount = reader.NumberOfPages; //Loop through each page for (int i = 1; i <= pageCount; i++) { //Get the current page pageDictionary = reader.GetPageN(i); //Get all of the annotations for the current page annots = pageDictionary.GetAsArray(PdfName.ANNOTS); //Make sure we have something if ((annots == null) || (annots.Length == 0)) { Console.WriteLine("nothing"); } //Loop through each annotation if (annots != null) { //add page number to list links.Add("Page " + i); foreach (PdfObject A in annots.ArrayList) { //Convert the itext-specific object as a generic PDF object PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(A); //Make sure this annotation has a link if (!AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK)) { continue; } //Make sure this annotation has an ACTION if (AnnotationDictionary.Get(PdfName.A) == null) { continue; } //Get the ACTION for the current annotation PdfDictionary AnnotationAction = AnnotationDictionary.GetAsDict(PdfName.A); // Test if it is a URI action if (AnnotationAction.Get(PdfName.S).Equals(PdfName.URI)) { PdfString Destination = AnnotationAction.GetAsString(PdfName.URI); string url = Destination.ToString(); //add url to list links.Add(url); } } } } } else if ((fileExt == "xls") || (fileExt == "xlsx")) { Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); excelApp.Visible = false; //open excel file Workbook excelWorkbook = excelApp.Workbooks.Open(filename, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false); Sheets excelSheets = excelWorkbook.Worksheets; //loop through worksheets for (int i = 1; i <= excelSheets.Count; i++) { Worksheet sheet = (Worksheet)excelSheets.Item[i]; //extract links if (sheet.Hyperlinks.Count > 0) { links.Add("Page " + i); for (int j = 1; j <= sheet.Hyperlinks.Count; j++) { string address = sheet.Hyperlinks[i].Address; links.Add(address); } } } excelWorkbook.Close(false); excelApp.Quit(); } else if ((fileExt == "doc") || (fileExt == "docx")) { Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Document doc = wordApp.Documents.Open(FileName: filename, ReadOnly: true); Microsoft.Office.Interop.Word.Hyperlinks hyperlinks = doc.Hyperlinks; if (hyperlinks.Count > 0) { //links.Add("Page: "); foreach (var hyperlink in hyperlinks) { string address = ((Microsoft.Office.Interop.Word.Hyperlink)hyperlink).Address; links.Add(address); } } doc.Close(false); wordApp.Quit(false); } else if ((fileExt == "csv") || (fileExt == "txt")) { string[] lines = System.IO.File.ReadAllLines(filename); foreach (string line in lines) { foreach (Match item in Regex.Matches(line, @"(http(s)?://)?([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?")) { string address = item.Value; links.Add(address); } } } else { } //write the file's link count to the log file //have to subtract the page number markers from the links count int fileLinkCount = 0; foreach (string line in links) { if (line.Substring(0, 4) != "Page") { fileLinkCount++; } } System.IO.File.AppendAllText(logFilePath, "Link count: " + fileLinkCount + Environment.NewLine); //write the file's links to the log file if (links.Count > 0) { using (TextWriter tw = new StreamWriter(logFilePath, append: true)) { foreach (String s in links) { tw.WriteLine(s); } } } System.IO.File.AppendAllText(logFilePath, "----------" + Environment.NewLine); } } }
public object save() { string templateFile = "/Users/user/Downloads/1022out.pdf"; string outFile = "/Users/user/Downloads/1022out.html"; // ConvertPdfStreamToHtml(); iTextSharp.text.pdf.PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new iTextSharp.text.pdf.PdfReader(templateFile); PRAcroForm s = pdfReader.AcroForm; AcroFields pdfFormFields = pdfReader.AcroFields; foreach (var item in pdfFormFields.Fields) { var d = item.Value.GetMerged(0); int type = pdfFormFields.GetFieldType(item.Key); string aaac = pdfFormFields.GetField(item.Key); PRAcroForm.FieldInformation aaad = s.GetField(item.Key); PdfString aae = aaad.Info.GetAsString(PdfName.TU); if (aae == null) { continue; } // Console.WriteLine(item.Key+":"+type.ToString); Console.WriteLine("===={0},{1},{2},{3}===", item.Key, type, aaac, aae.ToString()); if (type == 2) { string[] aaa = pdfFormFields.GetAppearanceStates(item.Key); Console.WriteLine("{0}", string.Join(",", aaa)); } // PrintProperties(item); var str = d.Get(PdfName.V); if (!string.IsNullOrEmpty(str?.ToString())) { var p = (str.GetBytes()[0] << 8) + str.GetBytes()[1]; string code; switch (p) { case 0xefbb: code = "UTF-8"; break; case 0xfffe: code = "Unicode"; break; case 0xfeff: code = "UTF-16BE"; break; default: code = "GBK"; break; } var value = Encoding.GetEncoding(code).GetString(str.GetBytes()); Console.WriteLine(item.Key + ":" + value); } } } catch (Exception ex) { Console.WriteLine("" + ex.Message); } finally { if (pdfStamper != null) { pdfStamper.Close(); } if (pdfReader != null) { pdfReader.Close(); } } return(DateTime.Now); }
/// <summary> /// ChangeAnnotations /// </summary> /// <param name="inputPath"></param> /// <param name="outputPath"></param> void ChangeAnnotations(string inputPath, string outputPath) { if (DGV_Info.DataSource == null) { MessageBox.Show("Input source is Grid. Please fill Grid"); return; } PdfReader reader = new PdfReader(inputPath); PdfStamper pdfStamper = new PdfStamper(reader, new FileStream(outputPath, FileMode.Create)); //// for (int i = 1; i <= reader.NumberOfPages; i++) { PdfArray array = reader.GetPageN(i).GetAsArray(PdfName.ANNOTS); if (array == null) { continue; } for (int j = 0; j < array.Size; j++) { PdfDictionary annot = array.GetAsDict(j); List <PdfName> keys = annot.Keys.ToList(); if (annot != null || (annot.Length != 0)) { if (annot.Get(PdfName.SUBTYPE).Equals(PdfName.CIRCLE)) { PdfString comment = annot.GetAsString(PdfName.CONTENTS); PdfString author = annot.GetAsString(PdfName.T); var subj = keys.Find(x => x.ToString() == "/Subj"); var subject = annot.Get(subj); if ((author != null) && (author.ToString().Contains("Legend"))) { continue; } if (curXCELType == "Access Control") { if ((comment != null) && (comment.ToString().Contains("AC"))) { var cmtSegs = comment.ToString().Split(' '); var acNum = 0; foreach (string c in cmtSegs) { if (c.Contains("AC")) { string acNo = c.Substring(c.IndexOf("AC") + 2); if (Int32.TryParse(acNo, out int x)) { // you know that the parsing attempt was successful acNum = x; } break; } } if (acNum == 0) { MessageBox.Show("Could not get AC number from comment. Need Format AC# where # is between 1 and 999"); } var IPAMQry = from DataGridViewRow rowView in DGV_Info.Rows select rowView; /* * var testQry = * from DataGridViewRow rowView in dgvRr * where ( * (string.Compare(rowView.Cells["LINE"].Value.ToString(), vrMSCLineNo) == 0) && * (string.Compare(rowView.Cells["SPECIFIC_NUMBERS"].Value.ToString(), "") != 0) * ) * select rowView; */ foreach (var row in IPAMQry) { var acRow = row.Cells[0].Value.ToString(); if (acNum.ToString() == acRow) { var updateComment = "AC" + row.Cells[0].Value.ToString() + "; " + row.Cells[1].Value.ToString() + "; " + row.Cells[8].Value.ToString(); annot.Put(PdfName.CONTENTS, new PdfString(updateComment.ToString())); } } } } if (curXCELType == "CCTV") { if ((comment != null) && (comment.ToString().Contains("C-"))) { var cmtSegs = comment.ToString().Split(' '); var acNum = 0; foreach (string c in cmtSegs) { if (c.Contains("C-")) { string acNo = c.Substring(c.IndexOf("C-") + 2); if (Int32.TryParse(acNo, out int x)) { // you know that the parsing attempt was successful acNum = x; } break; } } if (acNum == 0) { MessageBox.Show("Could not get Camera number (C-) from comment. Need Format AC# where # is between 1 and 999"); } var IPAMQry = from DataGridViewRow rowView in DGV_Info.Rows select rowView; /* * var testQry = * from DataGridViewRow rowView in dgvRr * where ( * (string.Compare(rowView.Cells["LINE"].Value.ToString(), vrMSCLineNo) == 0) && * (string.Compare(rowView.Cells["SPECIFIC_NUMBERS"].Value.ToString(), "") != 0) * ) * select rowView; */ foreach (var row in IPAMQry) { var acRow = row.Cells[0].Value.ToString(); if (acNum.ToString() == acRow) { var updateComment = "C-" + row.Cells[0].Value.ToString() + "; " + row.Cells[1].Value.ToString() + "; " + row.Cells[8].Value.ToString(); annot.Put(PdfName.CONTENTS, new PdfString(updateComment.ToString())); } } } } } } } } pdfStamper.Close(); }
/// <summary> /// ReadPDFAnnotations() /// </summary> /// <param name="PDF"></param> private void ReadPDFAnnotations(string PDF) { PdfReader reader = new PdfReader(PDF); int cnt = 0; for (int i = 1; i <= reader.NumberOfPages; i++) { PdfArray array = reader.GetPageN(i).GetAsArray(PdfName.ANNOTS); if (array == null) { continue; } for (int j = 0; j < array.Size; j++) { PdfDictionary annot = array.GetAsDict(j); List <PdfName> keys = annot.Keys.ToList(); if (annot != null || (annot.Length != 0)) { if (annot.Get(PdfName.SUBTYPE).Equals(PdfName.CIRCLE)) { PdfString comment = annot.GetAsString(PdfName.CONTENTS); PdfString author = annot.GetAsString(PdfName.T); var subj = keys.Find(x => x.ToString() == "/Subj"); var subject = annot.Get(subj); if (author != null) { if (author.ToString().Contains("Legend")) { continue; } if (author.ToString().Contains("View")) { continue; } if (author.ToString().Contains("Monitor")) { continue; } } cnt += 1; myPDFClass temp = new myPDFClass { AnnotID = cnt, Comment = (comment != null) ? Convert.ToString(comment).Trim() : "", Author = (author != null) ? Convert.ToString(author).Trim() : "null!", Subject = (subject != null) ? Convert.ToString(subject).Trim() : "null!" }; myPDFList.Add(temp); } } } } reader.Close(); }
public ActionResult Index() { #region [Search Annotations] List <AnnotC> Annot = new List <AnnotC>(); PdfReader vAnnotedReader = new PdfReader(Server.MapPath("~/Data/Extender.pdf")); for (int i = 1; i <= vAnnotedReader.NumberOfPages; i++) { PdfArray array = vAnnotedReader.GetPageN(i).GetAsArray(PdfName.ANNOTS); if (array == null) { continue; } //AnnotC c = new AnnotC(); //c.Page = i; //c.Annot = vAnnotedReader.GetPageN(i); //Annot.Add(c); for (int j = 0; j < array.Size; j++) { PdfDictionary annot = array.GetAsDict(j); PdfString text = annot.GetAsString(PdfName.CONTENTS); if (text != null) { AnnotC c = new AnnotC(); c.Page = i; c.Annot = annot; Annot.Add(c); } } } PdfReader vAnnotedReader2 = new PdfReader(Server.MapPath("~/Data/Ori.pdf")); for (int i = 1; i <= vAnnotedReader2.NumberOfPages; i++) { PdfArray array = vAnnotedReader2.GetPageN(i).GetAsArray(PdfName.ANNOTS); if (array == null) { continue; } for (int j = 0; j < array.Size; j++) { PdfDictionary annot = array.GetAsDict(j); PdfString text = annot.GetAsString(PdfName.CONTENTS); if (text != null) { AnnotC c = new AnnotC(); c.Page = i; c.Annot = annot; Annot.Add(c); } //PdfAnnotation test = annot; } } //var json = new JavaScriptSerializer().Serialize(Annot); PdfReader vOriginalReader = new PdfReader(Server.MapPath("~/Data/Ori.pdf")); var pDestinationFileName = Server.MapPath("~/Data/Merge.pdf"); for (int vPageNumber = 1; vPageNumber <= vOriginalReader.NumberOfPages; vPageNumber++) { PdfDictionary vOriginalPage = vOriginalReader.GetPageN(vPageNumber); foreach (var x in Annot) { if (x.Page == vPageNumber) { //PdfAnnotation annotation = PdfAnnotation.CreateText(vStamper.Writer, x.Annot.GetAsName(PdfRectangle.ARRAY), "Note", "This document is written by Dara Yuk", false, "Note"); PdfString contents = x.Annot.GetAsString(PdfName.CONTENTS); String value = contents.ToString(); Rectangle rect = Rectangle() //vOriginalReader.GetPageN(vPageNumber).MergeDifferent(x.Annot); //PdfAnnotation test = x.Annot.GetAsString(PdfName.ann) vOriginalReader.GetPageN(vPageNumber).Put(PdfName.ANNOT, x.Annot); //vOriginalReader.GetPageN(vPageNumber).Merge(x.Annot); } } //vStamper.MarkUsed(vOriginalPage); } PdfStamper vStamper = new PdfStamper(vOriginalReader, new FileStream(pDestinationFileName, FileMode.Create)); vStamper.Close(); vAnnotedReader.Close(); vOriginalReader.Close(); #endregion #region [Merging] /*//set the output file * using (Stream outputPdfStream = new FileStream("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Merge.pdf", FileMode.Create, FileAccess.Write, FileShare.None)) * { * //select the PDF files you want to merge, in this example I only merged 2 files, but you can do more. * string[] pdfArrayFiles = { "C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Extender.pdf", "C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Ori.pdf" }; * * //create a new document * Document document = new Document(); * //use PdfSmartCopy to merge PDF files to output file (or stream) * PdfSmartCopy copy = new PdfSmartCopy(document, outputPdfStream); * //open the document * document.Open(); * //set the reader to read files and add pages * PdfReader reader; * int n; * for (int i = 0; i < pdfArrayFiles.Length; i++) * { * reader = new PdfReader(pdfArrayFiles[i]); * n = reader.NumberOfPages; * for (int page = 0; page < n;) * { * copy.AddPage(copy.GetImportedPage(reader, ++page)); * } * } * document.Close(); * }*/ #endregion #region [3] //Stream sourcePdfStream = new FileStream("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Extender.pdf", FileMode.Create, FileAccess.Write, FileShare.None); //Stream destinationPdfStream = new FileStream("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Ori.pdf", FileMode.Create, FileAccess.Write, FileShare.None); //var output = new MemoryStream(); //using (var document = new Document(PageSize.A4, 70f, 70f, 20f, 20f)) //{ // var readers = new List<PdfReader>(); // var writer = PdfWriter.GetInstance(document, output); // writer.CloseStream = false; // document.Open(); // const Int32 requiredWidth = 500; // const Int32 zeroBottom = 647; // const Int32 left = 50; // Action<String, Action> inlcudePdfInDocument = (filename, e) => // { // var reader = new PdfReader(filename); // readers.Add(reader); // var pageCount = reader.NumberOfPages; // for (var i = 0; i < pageCount; i++) // { // e?.Invoke(); // var imp = writer.GetImportedPage(reader, (i + 1)); // var scale = requiredWidth / imp.Width; // var height = imp.Height * scale; // writer.DirectContent.AddTemplate(imp, scale, 0, 0, scale, left, zeroBottom - height); // var annots = reader.GetPageN(i + 1).GetAsArray(PdfName.ANNOTS); // if (annots != null && annots.Size != 0) // { // foreach (var a in annots) // { // var newannot = new PdfAnnotation(writer, new Rectangle(0, 0)); // var annotObj = (PdfDictionary)PdfReader.GetPdfObject(a); // newannot.PutAll(annotObj); // var rect = newannot.GetAsArray(PdfName.RECT); // rect[0] = new PdfNumber(((PdfNumber)rect[0]).DoubleValue * scale + left); // Left // rect[1] = new PdfNumber(((PdfNumber)rect[1]).DoubleValue * scale); // top // rect[2] = new PdfNumber(((PdfNumber)rect[2]).DoubleValue * scale + left); // right // rect[3] = new PdfNumber(((PdfNumber)rect[3]).DoubleValue * scale); // bottom // writer.AddAnnotation(newannot); // } // } // document.NewPage(); // } // }; // List<String> pdfs = new List<string>(); // pdfs.Add("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Extender.pdf"); // pdfs.Add("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Ori.pdf"); // foreach (var apprPdf in pdfs) // { // document.NewPage(); // inlcudePdfInDocument(apprPdf, null); // } // document.Close(); // readers.ForEach(x => x.Close()); //} //output.Position = 0; //FileStream F = new FileStream("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Merge.pdf", FileMode.Create, FileAccess.Write, FileShare.None); //output.WriteTo(F); //F.Close(); //output.Close(); #endregion #region [Add Annot] //path to source file //String source = "C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Extender.pdf"; ////create PdfReader object to read the source file //PdfReader reader = new PdfReader(source); ////create PdfStamper object to modify the content of the PDF //PdfStamper stamp = new PdfStamper(reader, new FileStream("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Extender - Copy.pdf", FileMode.Create)); ////create a Rectangle object to deine the size and location of the annotation //Rectangle rect = new Rectangle(10, 30, 50, 50); ////create a PdfAnnotation object //PdfAnnotation annotation = PdfAnnotation.CreateText(stamp.Writer, rect, "Note", "This document is written by Dara Yuk", false, "Note"); ////loop through the document and add the annotation to every page //for (int page = 1; page <= reader.NumberOfPages; page++) // stamp.AddAnnotation(annotation, page); //stamp.Close(); //System.Diagnostics.Process.Start("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Extender - Copy.pdf"); //Console.Read(); #endregion #region [Tes Copy Annot] //Stream sourcePdfStream = new FileStream("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Extender.pdf", FileMode.Create, FileAccess.Write, FileShare.None); //Stream destinationPdfStream = new FileStream("C:\\Users\\Athe\\source\\repos\\Reader\\Reader\\Data\\Ori.pdf", FileMode.Create, FileAccess.Write, FileShare.None); //// Create new document (IText) //Document outdoc = new Document(PageSize.A4); //// Seek to Stream start and create Reader for input PDF //m.Seek(0, SeekOrigin.Begin); //PdfReader inputPdfReader = new PdfReader(sourcePdfStream); //// Seek to Stream start and create Reader for destination PDF //m.Seek(0, SeekOrigin.Begin); //PdfReader destinationPdfReader = new PdfReader(destinationPdfStream); //// Create a PdfWriter from for new a pdf destination stream //// You should write into a new stream here! //Stream processedPdf = new MemoryStream(); //PdfWriter pdfw = PdfWriter.GetInstance(outdoc, processedPdf); //// do not close stream if we've read everything //pdfw.CloseStream = false; //// Open document //outdoc.Open(); //// get number of pages //int numPagesIn = inputPdfReader.NumberOfPages; //int numPagesOut = destinationPdfReader.NumberOfPages; //int max = numPagesIn; //// Process max number of pages //if (max < numPagesOut) //{ // throw new Exception("Impossible - different number of pages"); //} //int i = 0; //// Process Pdf pages //while (i < max) //{ // // Import pages from corresponding reader // PdfImportedPage pageIn = writer.inputPdfReader(reader, i); // PdfImportedPage pageOut = writer.destinationPdfReader(reader, i); // // Get named destinations (annotations // List<Annotat ions> toBeAdded = ParseInAndOutAndGetAnnotations(pageIn, pageOut); // // add your annotations // foreach (Annotation anno in toBeAdded) pageOut.Add(anno); // // Add processed page to output PDFWriter // outdoc.Add(pageOut); //} //// PDF creation finished //outdoc.Close(); #endregion #region [Test Copy Annot 2] //string oldFile = Server.MapPath("~/Data2/Extender.pdf"); //string oldFile2 = Server.MapPath("~/Data2/Ori.pdf"); //string newFile = Server.MapPath("~/Data2/Merge.pdf"); //// open the reader //PdfReader reader = new PdfReader(oldFile); //Rectangle size = reader.GetPageSizeWithRotation(1); //Document document = new Document(size); //PdfReader reader2 = new PdfReader(oldFile2); //Rectangle size2 = reader2.GetPageSizeWithRotation(1); //Document document2 = new Document(size2); //// open the writer //// remember to set the page size before opening document //// otherwise the page is already set. ///* chapter02/HelloWorldMetadata.java */ //document.Open(); //// the pdf content //// cb does not work with stamper //// create the new pagez and add it to the pdf //// this segment of code is meant for writer //FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.ReadWrite); //PdfStamper writer = new PdfStamper(reader, fs, reader.PdfVersion, false); //for (int pg = 1; pg <= reader.NumberOfPages; pg++) //{ // // taken from http://itextsharp.10939.n7.nabble.com/How-to-edit-annotations-td3352.html // PdfDictionary pagedic = reader.GetPageN(pg); // PdfArray annotarray = (PdfArray)PdfReader.GetPdfObject(pagedic.Get(PdfName.ANNOTS)); // if (annotarray == null || annotarray.Size == 0) // continue; // foreach (PdfIndirectReference annot in annotarray.ArrayList) // { // PdfDictionary annotationDic = (PdfDictionary)PdfReader.GetPdfObject(annot); // PdfName subType = (PdfName)annotationDic.Get(PdfName.SUBTYPE); // if (subType.Equals(PdfName.TEXT) || subType.Equals(PdfName.FREETEXT)) // { // annotationDic.Put(PdfName.CONTENTS, new PdfString("These are changed contents", PdfObject.TEXT_UNICODE)); // } // PdfString contents = annotationDic.GetAsString(PdfName.CONTENTS); // if (contents != null) // { // String value = contents.ToString(); // annotationDic.Put(PdfName.CONTENTS, new PdfString(value)); // annotationDic.Remove(PdfName.AP); // List<PdfName> tobeDel = new List<PdfName>(); // foreach (PdfName key in annotationDic.Keys) // { // if (key.CompareTo(PdfName.AP) == 0 || // key.CompareTo(PdfName.RC) == 0 || // annotationDic.Get(key).IsDictionary()) // { // tobeDel.Add(key); // } // } // foreach (PdfName key in tobeDel) // { // annotationDic.Remove(key); // } // } // writer.MarkUsed(annotationDic); // } // if ((pg + 1) < reader.NumberOfPages) // { // document.NewPage(); // } //} ////tesstart //PdfStamper writer2 = new PdfStamper(reader2, fs, reader2.PdfVersion, false); //document2.Open(); //for (int pg = 1; pg <= reader2.NumberOfPages; pg++) //{ // PdfDictionary pagedic = reader2.GetPageN(pg); // PdfArray annotarray = (PdfArray)PdfReader.GetPdfObject(pagedic.Get(PdfName.ANNOTS)); // if (annotarray == null || annotarray.Size == 0) // continue; // foreach (PdfIndirectReference annot in annotarray.ArrayList) // { // PdfDictionary annotationDic = (PdfDictionary)PdfReader.GetPdfObject(annot); // PdfName subType = (PdfName)annotationDic.Get(PdfName.SUBTYPE); // if (subType.Equals(PdfName.TEXT) || subType.Equals(PdfName.FREETEXT)) // { // annotationDic.Put(PdfName.CONTENTS, new PdfString("These are changed contents", PdfObject.TEXT_UNICODE)); // } // PdfString contents = annotationDic.GetAsString(PdfName.CONTENTS); // if (contents != null) // { // String value = contents.ToString(); // annotationDic.Put(PdfName.CONTENTS, new PdfString(value)); // annotationDic.Remove(PdfName.AP); // List<PdfName> tobeDel = new List<PdfName>(); // foreach (PdfName key in annotationDic.Keys) // { // if (key.CompareTo(PdfName.AP) == 0 || // key.CompareTo(PdfName.RC) == 0 || // annotationDic.Get(key).IsDictionary()) // { // tobeDel.Add(key); // } // } // foreach (PdfName key in tobeDel) // { // annotationDic.Remove(key); // } // } // writer2.MarkUsed(annotationDic); // } // if ((pg + 1) < reader2.NumberOfPages) // { // document2.NewPage(); // } //} ////tesend //// close the streams and voilá the file should be changed :) //writer.Close(); //reader.Close(); #endregion ViewBag.Title = "Home Page"; return(View()); }
/** * Converts an annotation structure item to a Form XObject annotation. * @param item the structure item * @throws IOException */ protected void ConvertToXObject(StructureObject item) { PdfDictionary structElem = item.GetStructElem(); if (structElem == null) { return; } PdfDictionary dict = item.GetObjAsDict(); if (dict == null || !dict.CheckType(PdfName.ANNOT)) { return; } PdfDictionary ap = dict.GetAsDict(PdfName.AP); if (ap == null) { return; } PdfNumber structParent = dict.GetAsNumber(PdfName.STRUCTPARENT); if (structParent == null) { return; } PdfStream stream = ap.GetAsStream(PdfName.N); if (stream == null) { return; } stream.Put(PdfName.STRUCTPARENT, structParent); PdfIndirectReference xobjr = ap.GetAsIndirectObject(PdfName.N); if (xobjr == null) { return; } // remove the annotation from the page for (int i = 0; i < annots.Length; i++) { PdfIndirectReference annotref = annots.GetAsIndirectObject(i); if (item.GetObjRef().Number == annotref.Number) { annots.Remove(i); break; } } // replace the existing attributes by a PrintField attribute PdfDictionary attribute = new PdfDictionary(); attribute.Put(PdfName.O, PdfName.PRINTFIELD); PdfString description = dict.GetAsString(PdfName.TU); if (description == null) { description = dict.GetAsString(PdfName.T); } if (PdfName.BTN.Equals(dict.Get(PdfName.FT))) { PdfNumber fflags = dict.GetAsNumber(PdfName.FF); if (fflags != null) { int ff = fflags.IntValue; if ((ff & PdfFormField.FF_PUSHBUTTON) != 0) { attribute.Put(PdfName.ROLE, PdfName.PB); } // I don't think the condition below will ever be true if ((ff & PdfFormField.FF_RADIO) != 0) { attribute.Put(PdfName.ROLE, PdfName.rb); } else { attribute.Put(PdfName.ROLE, PdfName.CB); } } } else { attribute.Put(PdfName.ROLE, PdfName.TV); } attribute.Put(PdfName.DESC, description); // Updating the values of the StructElem dictionary PdfString t = structElem.GetAsString(PdfName.T); if (t == null || t.ToString().Trim().Length == 0) { structElem.Put(PdfName.T, dict.GetAsString(PdfName.T)); } structElem.Put(PdfName.A, attribute); structElem.Put(PdfName.S, PdfName.P); structElem.Put(PdfName.PG, pageref); // Defining a new MCID int mcid = items.ProcessMCID(structParents, item.GetRef()); LOGGER.Info("Using MCID " + mcid); structElem.Put(PdfName.K, new PdfNumber(mcid)); // removing the annotation from the parent tree items.RemoveFromParentTree(structParent); // Adding the XObject to the page PdfName xobj = new PdfName("XObj" + structParent.IntValue); LOGGER.Info("Creating XObject with name " + xobj); xobjects.Put(xobj, xobjr); PdfArray array = dict.GetAsArray(PdfName.RECT); // Getting the position of the annotation Rectangle rect = new Rectangle( array.GetAsNumber(0).FloatValue, array.GetAsNumber(1).FloatValue, array.GetAsNumber(2).FloatValue, array.GetAsNumber(3).FloatValue); rect.Normalize(); // A Do operator is forbidden inside a text block if (inText && !btWrite) { LOGGER.Debug("Introducing extra ET"); byte[] bytes = Encoding.ASCII.GetBytes("ET\n"); baos.Write(bytes, 0, bytes.Length); etExtra = true; } // Writing the marked-content sequence with the Do operator // Note that the position assumes that the CTM wasn't changed in the graphics state // TODO: do the math if the CTM did change! ByteBuffer buf = new ByteBuffer(); buf.Append("/P <</MCID "); buf.Append(mcid); buf.Append(">> BDC\n"); buf.Append("q 1 0 0 1 "); buf.Append(rect.Left.ToString(CultureInfo.InvariantCulture)); buf.Append(" "); buf.Append(rect.Bottom.ToString(CultureInfo.InvariantCulture)); buf.Append(" cm "); buf.Append(xobj.GetBytes()); buf.Append(" Do Q\n"); buf.Append("EMC\n"); buf.Flush(); buf.WriteTo(baos); // if we were inside a text block, we've introduced an ET, so we'll need to write a BT if (inText) { btWrite = true; } }
public void OneFile_LinksToTheNextFile_UpdatesLink(string exeFileName) { HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName); string htmlFile2Contents = @" <html> <head> </head> <body> Page 2 </body> </html>"; using (TempHtmlFile htmlFile2 = new TempHtmlFile(htmlFile2Contents)) { string htmlFile1Contents = $@" <html> <head> </head> <body> Page 1 <br/> <a href=""{htmlFile2.FilePath}"">Page 2</a> </body> </html>"; using (TempHtmlFile htmlFile1 = new TempHtmlFile(htmlFile1Contents)) { using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext)) { string commandLine = $"\"{htmlFile1.FilePath}\" \"{htmlFile2.FilePath}\" \"{pdfFile.FilePath}\""; HtmlToPdfRunResult result = runner.Run(commandLine); Assert.AreEqual(0, result.ExitCode, result.Output); using (PdfReader pdfReader = new PdfReader(pdfFile.FilePath)) { using (PdfDocument pdfDocument = new PdfDocument(pdfReader)) { Assert.AreEqual(2, pdfDocument.GetNumberOfPages()); // get the first page PdfPage pdfPage = pdfDocument.GetPage(1); // get link annotations List <PdfLinkAnnotation> linkAnnotations = pdfPage.GetAnnotations().OfType <PdfLinkAnnotation>().ToList(); Assert.AreEqual(1, linkAnnotations.Count); // get the first link annotation PdfLinkAnnotation linkAnnotation = linkAnnotations.ElementAt(0); Assert.IsNotNull(linkAnnotation); // get action PdfDictionary action = linkAnnotation.GetAction(); Assert.IsNotNull(action); // get GoTo sub-type PdfName s = action.GetAsName(PdfName.S); if (exeFileName == HtmlToPdfRunner.HtmlToPdfExe) { Assert.AreEqual(PdfName.GoTo, s); // get destination PdfArray destination = action.GetAsArray(PdfName.D); PdfIndirectReference destinationPageReference = destination.GetAsDictionary(0).GetIndirectReference(); PdfName zoom = destination.GetAsName(1); PdfNumber pageOffset = destination.GetAsNumber(2); // get expected values PdfPage pdfPage2 = pdfDocument.GetPage(2); PdfDictionary page2Dictionary = pdfPage2.GetPdfObject(); PdfIndirectReference expectedPageReference = page2Dictionary.GetIndirectReference(); PdfName expectedZoom = PdfName.FitH; float expectedPageOffset = pdfPage2.GetPageSize().GetTop(); // assert Assert.AreEqual(expectedPageReference, destinationPageReference); Assert.AreEqual(expectedZoom, zoom); Assert.AreEqual(expectedPageOffset, pageOffset.FloatValue()); } else if (exeFileName == HtmlToPdfRunner.WkhtmltopdfExe) { Assert.AreEqual(PdfName.URI, s); PdfString uri = action.GetAsString(PdfName.URI); Assert.AreEqual(htmlFile2.FilePath, HttpUtility.UrlDecode(uri.ToString())); } } } } } } }