當前位置: 首頁>>代碼示例>>Java>>正文


Java PdfWriter.addAnnotation方法代碼示例

本文整理匯總了Java中com.itextpdf.text.pdf.PdfWriter.addAnnotation方法的典型用法代碼示例。如果您正苦於以下問題:Java PdfWriter.addAnnotation方法的具體用法?Java PdfWriter.addAnnotation怎麽用?Java PdfWriter.addAnnotation使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.itextpdf.text.pdf.PdfWriter的用法示例。


在下文中一共展示了PdfWriter.addAnnotation方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testAnnotationIconForTYD

import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
/**
 * <a href="https://stackoverflow.com/questions/46204693/cant-get-itext-rectangle-to-work-correctly-with-annotations">
 * Can't get itext Rectangle to work correctly with annotations
 * </a>
 * <p>
 * This test looks at a <b>Text</b> annotation added via a {@link Chunk}
 * as done by the OP. As this way of adding annotations resets the
 * annotation <b>Rect</b> to the bounding box of the rendered {@link Chunk},
 * it is not really what the OP wants.
 * </p>
 */
@Test
public void testAnnotationIconForTYD() throws FileNotFoundException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "annotationIcons.pdf")));
    document.open();

    // Not "new Rectangle(164, 190, 164, 110)" which would be empty
    Rectangle rect = new Rectangle(164, 190, 328, 300);

    // Annotation added like the OP does
    Chunk chunk_text = new Chunk("Let's test a Text annotation...");
    chunk_text.setAnnotation(PdfAnnotation.createText(writer, rect, "Warning", "This is a Text annotation with Comment icon.", false, "Comment"));        

    document.add(chunk_text);

    // Annotation added to the document without Chunk
    writer.addAnnotation(PdfAnnotation.createText(writer, rect, "Warning 2", "This is another Text annotation with Comment icon.", false, "Comment"));

    document.close();
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:32,代碼來源:AnnotationIcons.java

示例2: testCreateFormWithSameFieldTwice

import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
/**
 * <a href="http://stackoverflow.com/questions/28943245/possible-to-ues-variables-in-a-pdf-doument">
 * Possible to ues variables in a PDF doument?
 * </a>
 * <p>
 * Generates a sample PDF containing two widgets of the same text field.
 * </p>
 */
@Test
public void testCreateFormWithSameFieldTwice() throws IOException, DocumentException
{
    try (   OutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "aFieldTwice.pdf"))  )
    {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, os);
        document.open();
        document.add(new Paragraph("The same field twice"));
        
        PdfFormField field = PdfFormField.createTextField(writer, false, false, 0);
        field.setFieldName("fieldName");

        PdfFormField annot1 = PdfFormField.createEmpty(writer);
        annot1.setWidget(new Rectangle(30, 700, 200, 720), PdfAnnotation.HIGHLIGHT_INVERT);
        field.addKid(annot1);

        PdfFormField annot2 = PdfFormField.createEmpty(writer);
        annot2.setWidget(new Rectangle(230, 700, 400, 720), PdfAnnotation.HIGHLIGHT_INVERT);
        field.addKid(annot2);

        writer.addAnnotation(field);
        
        document.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:35,代碼來源:SameFieldTwice.java

示例3: testCreateLinkWithAppearance

import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
/**
 * <a href="http://stackoverflow.com/questions/34734669/define-background-color-and-transparency-of-link-annotation-in-pdf">
 * Define background color and transparency of link annotation in PDF
 * </a>
 * <p>
 * This test creates a link annotation with custom appearance. Adobe Reader chooses
 * to ignore it but other viewers use it. Interestingly Adobe Acrobat export-as-image
 * does use the custom appearance...
 * </p>
 */
@Test
public void testCreateLinkWithAppearance() throws IOException, DocumentException
{
    Document doc = new Document();
    PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File(RESULT_FOLDER, "custom-link.appearance.pdf")));
    writer.setCompressionLevel(0);
    doc.open();

    BaseFont baseFont = BaseFont.createFont();
    int fontSize = 15;
    doc.add(new Paragraph("Hello", new Font(baseFont, fontSize)));
    
    PdfContentByte content = writer.getDirectContent();
    
    String text = "Test";
    content.setFontAndSize(baseFont, fontSize);
    content.beginText();
    content.moveText(100, 500);
    content.showText(text);
    content.endText();
    
    Rectangle linkLocation = new Rectangle(95, 495 + baseFont.getDescentPoint(text, fontSize),
            105 + baseFont.getWidthPoint(text, fontSize), 505 + baseFont.getAscentPoint(text, fontSize));

    PdfAnnotation linkGreen = PdfAnnotation.createLink(writer, linkLocation, PdfName.HIGHLIGHT, "green" );
    PdfTemplate appearance = PdfTemplate.createTemplate(writer, linkLocation.getWidth(), linkLocation.getHeight());
    PdfGState state = new PdfGState();
    //state.FillOpacity = .3f;
    // IMPROVEMENT: Use blend mode Darken instead of transparency; you may also want to try Multiply.
    state.setBlendMode(new PdfName("Darken"));
    appearance.setGState(state);

    appearance.setColorFill(BaseColor.GREEN);
    appearance.rectangle(0, 0, linkLocation.getWidth(), linkLocation.getHeight());
    appearance.fill();
    linkGreen.setAppearance(PdfName.N, appearance);
    writer.addAnnotation(linkGreen);

    doc.open();
    doc.close();
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:52,代碼來源:CreateLink.java


注:本文中的com.itextpdf.text.pdf.PdfWriter.addAnnotation方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。