2016-05-11 11 views
0

Ich habe Text in pdf hinzugefügt. Der hinzugefügte Text wird jedoch nicht im Browser angezeigt.Hinzugefügt Text in pdf nicht angezeigt im Browser mit itextsharp

using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) 
     { 
      using (PdfStamper stamper = new PdfStamper(reader, fs)) 
      { 
       for (int x = 1; x < reader.NumberOfPages; x++) 
       { 
        cb = stamper.GetUnderContent(x); 
        myLocationTextExtractionStrategy strategy = new myLocationTextExtractionStrategy(); 
        strategy.UndercontentCharacterSpacing = cb.CharacterSpacing; 
        strategy.UndercontentHorizontalScaling = cb.HorizontalScaling; 
        string currentText = PdfTextExtractor.GetTextFromPage(reader, x, strategy); 
        foreach (iTextSharp.text.Rectangle rect in MatchesFound) 
        { 
         //----------Annotation ---------------- 
         iTextSharp.text.Rectangle rect_txt = rect; 
         BaseFont helv = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 
         rect_txt.Bottom = rect_txt.Top - 2; 
         rect_txt.Top = rect_txt.Top + 8; 
         rect_txt.Right = rect_txt.Right - ((rect_txt.Width) - Convert.ToInt32(text_num.ToString().Length) * 5); 
         rect_txt.Left = rect_txt.Left - 3.0f; 
         PdfAnnotation comment = PdfAnnotation.CreateFreeText(stamper.Writer, rect_txt, text_num.ToString(), cb); 
         comment.PlaceInPage = x; 
         comment.Put(PdfName.RC, new PdfString("<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"Acrobat:11.0.13\" xfa:spec=\"2.0.2\" style=\"font-size:9.0pt;text-align:left;color:#FF0000;font-weight:bold;font-style:normal;font-family:'helvetica';font-stretch:normal\"><p dir=\"ltr\"><span style=\"font-family:helvetica\">" + text_num.ToString() + "</span></p></body>", PdfObject.TEXT_UNICODE)); 
         comment.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0); 
    stamper.AddAnnotation(comment, x); 
        } 
       } 
      } 
     } 
+0

die Antwort erfahren. Aussehen = PdfAppearance.CreateAppearance (stamper.Writer, rect_txt.Width, rect_txt.Height); appearance.Rectangle (0, 0, rect_txt.Width, rect_txt.Height); Aussehen.SetFontAndSize (helv, 12); appearance.SetColorFill (BaseColor.RED); ColumnText.ShowTextAligned (Darstellung, Element.ALIGN_LEFT, neue Phrase (text_num), 0, 0, 0); – Karthik

+0

Bitte posten Sie das als eine tatsächliche Antwort auf Ihre Frage, nicht nur ein Kommentar. – mkl

Antwort

1
//----------Annotation -------------- 

iTextSharp.text.Rectangle rect_txt = rect; 

rect_txt.Bottom = rect_txt.Top - 2; 
rect_txt.Top = rect_txt.Top + 8; 
rect_txt.Right = rect_txt.Right - ((rect_txt.Width) - Convert.ToInt32(text_num.ToString().Length) * 5); 
rect_txt.Left = rect_txt.Left - 3.0f; 

BaseFont helv = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 
appearance = PdfAppearance.CreateAppearance(stamper.Writer, rect_txt.Width, rect_txt.Height); 
appearance.Rectangle(0, 0, rect_txt.Width, rect_txt.Height); 
appearance.SetFontAndSize(helv, 12); 
appearance.SetColorFill(BaseColor.RED); 
ColumnText.ShowTextAligned(appearance, Element.ALIGN_LEFT, new Phrase(text_num), 0, 0, 0); 
//appearance.FillStroke(); 
// appearance.ShowTextAligned(Element.ALIGN_LEFT, text_num.ToString(), 0, 0, 0); 

PdfAnnotation comment = PdfAnnotation.CreateFreeText(stamper.Writer, rect_txt, text_num.ToString(), cb); 
comment.PlaceInPage = x; 
comment.Put(PdfName.RC, new PdfString("<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"Acrobat:11.0.13\" xfa:spec=\"2.0.2\" style=\"font-size:10px;text-align:left;color:#FF0000;font-weight:bold;font-style:normal;font-family:'helvetica';font-stretch:normal\"><p dir=\"ltr\"><span style=\"font-family:helvetica\">" + text_num.ToString() + "</span></p></body>", PdfObject.TEXT_UNICODE)); 
comment.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0); 
comment.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance); 
stamper.AddAnnotation(comment, x); 
+0

Bitte erläutern Sie Ihre Antwort und fügen Sie nicht einfach Code aus Ihrer IDE ein. Das Ziel ist, dass andere von Ihrer Antwort lernen können. Vielen Dank! –

Verwandte Themen