2017-09-07 28 views
0

私はPDFBoxとBoxableを初めて使用しています。この質問は、ここで質問された質問を参考にしています(Ref:https://github.com/dhorions/boxable/issues/89) flurinBooneaは、テキスト、画像、表をすべて同じページに配置するための小さなサンプルコードを提示しました。 私の質問は、(内部の内容に基づいて動的な高さを持つ)テーブルを作成し、テーブルの後ろにテキストを配置する必要がある場合です。どのように私はそれを行うことができますか? どこかは、私はテーブルを描きながら、私は次のコードの前にtable.drawを使用するたびに、私は次の要素のためのYPositionを得るために似たような、前に描画するとテーブルが消えるcontentStream - PDFBox with Boxable

float yPosition = table.draw() 

を使用し、次の要素のために、この位置を使用するが、その読み、

PDPageContentStream contentStream = new PDPageContentStream(doc, page); 
contentStream.beginText(); 
contentStream.setFont(font, 18); 
contentStream.moveTextPositionByAmount(0, yPosition - 20); 
contentStream.drawString("This is a test message"); 
contentStream.endText(); 
contentStream.close(); 

表が消え、テキストのみが表示されます。これを回避する方法がわかりません。誰かがこれで私を助けてくださいできますか?私はかなりの間この問題に悩まされています。したがって、あなたをこのコンテンツのストリームを作成する場合

/** 
* Create a new PDPage content stream. This constructor overwrites all existing content streams 
* of this page. 
* 
* @param document The document the page is part of. 
* @param sourcePage The page to write the contents to. 
* @throws IOException If there is an error writing to the page contents. 
*/ 
public PDPageContentStream(PDDocument document, PDPage sourcePage) throws IOException 

: は、あなたがこのコンストラクタは次のように文書化されて

PDPageContentStream contentStream = new PDPageContentStream(doc, page); 

を使用して、問題のページのための追加コンテンツストリームを作成し、事前

答えて

1

にありがとうあなたがそのページにあったすべてのコンテンツを放棄してください。

既存のコンテンツに追加する場合はいつでも、別のコンストラクタを使用してください。余談として

/** 
* Create a new PDPage content stream. 
* 
* @param document The document the page is part of. 
* @param sourcePage The page to write the contents to. 
* @param appendContent Indicates whether content will be overwritten, appended or prepended. 
* @param compress Tell if the content stream should compress the page contents. 
* @param resetContext Tell if the graphic context should be reset. This is only relevant when 
* the appendContent parameter is set to {@link AppendMode#APPEND}. You should use this when 
* appending to an existing stream, because the existing stream may have changed graphic 
* properties (e.g. scaling, rotation). 
* @throws IOException If there is an error writing to the page contents. 
*/ 
public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent, 
          boolean compress, boolean resetContext) throws IOException 

として文書化されて

PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true); 

:あなたは、あなたがいることを言及しので、私はあなたが特定のAに、現在のバージョンを使用すると仮定、PDFBoxとBoxableに新しいですPDFBox 2.0.x.何らかの理由で古いバージョン(1.8.xなど)を使用することを選択した場合は、代わりに

が必要です。

関連する問題