atjiu / pybbs

更实用的Java开发的社区(论坛),Better use of Java development community (forum)
GNU Affero General Public License v3.0
1.84k stars 706 forks source link

pdf support #56

Closed janewqiu closed 7 years ago

janewqiu commented 7 years ago

package example;

import java.io.IOException;

import org.apache.pdfbox.exceptions.COSVisitorException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentInformation; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; public class Step1 {

public static void main(String[] args) throws IOException, COSVisitorException
{

// if( args.length != 2 ) // { // System.err.println("usage: " + Step1.class.getName() + " "); // System.exit(1); // }

    String filename = "/tmp/pdf01.pdf";
    String message = "hello, world";

 // Create a document and add a page to it
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage( page );

    // Create a new font object selecting one of the PDF base fonts
    PDFont font = PDType1Font.HELVETICA_BOLD;

    PDDocumentInformation info = new PDDocumentInformation();
    info.setAuthor("Registered to: IBM-MANU");
    info.setCreator("HP Exstream Version 7.0.616 32-bit");
    info.setTitle("EOB");

    document.setDocumentInformation(info );

    // Start a new content stream which will "hold" the to be created content
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
    contentStream.beginText();
    contentStream.setFont( font, 12 );
    contentStream.moveTextPositionByAmount( 100, 700 );
    contentStream.drawString( "Hello World" );
    contentStream.endText();

    // Make sure that the content stream is closed:
    contentStream.close();

    // Save the results and ensure that the document is properly closed:
    document.save( "/tmp/hello.pdf");
    document.close();

    }
}

================= step2 package example; import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage;

import org.apache.pdfbox.pdmodel.PDPageContentStream;

import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class Step2 {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
      String filename = "/tmp/pdf01.pdf";
        String message = "hello, world";

        try (PDDocument doc = new PDDocument())
        {
            PDPage page = new PDPage();
            doc.addPage(page);

            PDFont font = PDType1Font.HELVETICA_BOLD;

            try (PDPageContentStream contents = new PDPageContentStream(doc, page))
            {
                contents.beginText();
                contents.setFont(font, 12);
                contents.newLineAtOffset(100, 700);
                contents.showText(message);
                contents.endText();
            }

            doc.save(filename);
        }

}

}

atjiu commented 7 years ago

首先,感谢分享 然后希望能写的详细点,把需要的包也加上,顺便把代码格式调一下