2017-09-04 17 views
1

PDFの特定の場所にテキストを配置するにはどうすればよいですか?私は少しの検索をしましたが、あまりにも良いものは見つかりませんでした。私はdocument.Add(new Paragraph("Date:" + DateTime.Now));を持っていて、私はそれをpdfファイルの特定の領域に置きたいと思っていました。iTextSharpを使用して段落を特定の場所に配置する方法

マイコード:

private void savePDF_Click(object sender, EventArgs e) 
    { 
     FileStream fileStream = new FileStream(nameTxtB.Text + "Repair.pdf", FileMode.Create, FileAccess.Write, FileShare.None); 
     Document document = new Document(); 
     document.Open(); 
     iTextSharp.text.Rectangle rectangle = new iTextSharp.text.Rectangle(PageSize.LETTER); 
     PdfWriter pdfWriter = PdfWriter.GetInstance(document, fileStream); 

     iTextSharp.text.Image r3tsLogo = iTextSharp.text.Image.GetInstance("rt3slogo.PNG"); //creates r3ts logo 
     iTextSharp.text.Image r3Info = iTextSharp.text.Image.GetInstance("R3 Information.PNG"); //creates r3 information text below r3ts logo 

     r3tsLogo.SetAbsolutePosition(document.PageSize.Width - 375 - 0f, document.PageSize.Height - 130 - 0f); 
     r3Info.SetAbsolutePosition(document.PageSize.Width - 365 - 0f, document.PageSize.Height - 170 - 0f); //higher the number in height the lower the place of text on paper 
            //less number will result in text more to right in width 

     //increase size of picture 
     r3tsLogo.ScalePercent(120); 
     r3Info.ScalePercent(65); 

//---------------adds all images to pdf file --------------------------------- 
     document.Add(r3tsLogo); 
     document.Add(r3Info); 
     document.Add(new Paragraph("Date:" + DateTime.Now)); 




     document.Close(); 
    } 

答えて

2

(ヨリスの答えを参照)あなたは絶対的な位置に画像を追加する方法を知っていると仮定すると、しかし、見てテキストを追加する方法は、あなたの質問への答えは:ColumnTextを使用しています。

あなただけがShowTextAligned()メソッドを使用することができ、ラップする必要はありません、単一の行を追加する必要がある場合:xyがために座標で、このコード行で

ColumnText.showTextAligned(writer.DirectContent, 
    Element.ALIGN_CENTER, new Phrase("single line"), x, y, rotation); 

をテキストの中央(他の可能なアラインメント値はALIGN_LEFTALIGN_RIGHTです)。 rotationパラメータは度での回転を定義します。テキスト"single line"はラップされません。追加するテキストが長すぎる場合、このように「ページからはみ出る」テキストを追加できます。

あなたが特定の矩形内のテキストを追加したい場合は、あなたがRectangleオブジェクトを使用して列を定義する必要があります。

ColumnText ct = new ColumnText(writer.DirectContent); 
ct.setSimpleColumn(new Rectangle(0, 0, 523, 50)); 
ct.addElement(new Paragraph("This could be a very long sentence that needs to be wrapped")); 
ct.go(); 

あなたが長方形に合ったよりも多くのテキストを提供する場合は、そのテキストが描画されることはありません。ただし、残りのテキストを別の位置に追加できるように、オブジェクトはctで使用できるようになります。

このすべてが尋ねたと前に回答されています

シングルライン:

複数行:

私はこれらの例のための長い検索する必要がありましたか?いいえ、私は公式のウェブサイトAbsolute Positioning of textの下でそれらを見つけました。+ DateTime.Now) `絶対位置で:

知恵は

1

この概念は徹底的ブック 'アクションでiTextの' で説明されています。これはウェブサイトで見つけることができます。

http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-3

ショートコードサンプル(他の例のためのサイトを確認してください):

// step 1 
Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30); 

// step 2 
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); 

// step 3 
document.open(); 

// step 4 
// Create and add a Paragraph 
Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22)); 
p.setAlignment(Element.ALIGN_CENTER); 
document.add(p); 

// Create and add an Image 
Image img = Image.getInstance(RESOURCE); 
img.setAbsolutePosition(
     (PageSize.POSTCARD.getWidth() - img.getScaledWidth())/2, 
     (PageSize.POSTCARD.getHeight() - img.getScaledHeight())/2); 
document.add(img); 
+0

は、私はこの問題は、画像を配置していないと思いますが、ポジショニング'「日付」...検索する人のためにそこにあります。もちろん、これは以前に何度も答えられてきた質問です。 –

関連する問題