Hello,

I am unable to splitt a PDF (with IText) and after getting the content (with
IText)...

Any ideas ?

Thanks

*To use this application, just place a PDF named "PDF_A.pdf" at the base of
the project.*
----------------------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.parser.PdfTextExtractor;

/**
 * Problem using iText
 * http://article.gmane.org/gmane.comp.java.lib.itext.general/46543
 */
public class TestReuseSameFileIText {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out
                .println("The problem is that after the creation of two
files,"
                        + " the second and last are unreadable and I don't
know what I do wrong..."
                        + " For my tests, I take one PDF (PDF_A) from 1
page, and I "
                        + "create the PDF_B. After I take PDF_B to create
PDF_C and "
                        + "there PDF_B and PDF_C are unreadable (with Code
using IText, but I can open"
                        + " and read it by the OS system). Test with IText
2.1.7."
                        + " \n !!! USAGE : put the file : PDF_A.pdf at the
base of the project.");
        TestReuseSameFileIText reuse = new TestReuseSameFileIText();

        // Put the original PDF at the base of the project.
        String PDF_A = "PDF_A.pdf";
        System.out.println("---------------iText " + PDF_A + "\n"
                + reuse.getTextWithIText(PDF_A));

        // Creation of PDF_B base on PDF_A
        String PDF_B = "PDF_B.pdf";
        reuse.splittPDF(PDF_A, PDF_B);
        System.out.println("---------------iText " + PDF_B + "\n"
                + reuse.getTextWithIText(PDF_B));

        // Creation of PDF_C base on PDF_B
        String PDF_C = "PDF_C.pdf";
        reuse.splittPDF(PDF_B, PDF_C);
        System.out.println("---------------iText " + PDF_C + "\n"
                + reuse.getTextWithIText(PDF_C));

        // There is bug because PDF_C has no content.... :(

    }

    /**
     * Take the file (just the first page) and recreate another (same
content).
     * Test with iText 2.1.7.
     *
     * @param fileName
     *            Source
     * @param nameOfNewFile
     *            Destination
     * @return
     */
    public boolean splittPDF(String fileName, String nameOfNewFile) {

        try {
            PdfReader reader = new PdfReader(fileName);

            com.lowagie.text.Document document = new
com.lowagie.text.Document(
                    reader.getPageSizeWithRotation(1));
            PdfWriter writer;
            try {
                File f = new File(nameOfNewFile);
                if (f.exists()) {
                    System.out.println("The file : " + nameOfNewFile
                            + " already exist. It will be overwrite");
                }
                writer = PdfWriter.getInstance(document, new
FileOutputStream(
                        nameOfNewFile));
            } catch (Exception e) {
                System.out.println(e);
                return false;
            }
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            PdfImportedPage page;
            int rotation;
            int pageNumber = 1;


document.setPageSize(reader.getPageSizeWithRotation(pageNumber));
            document.newPage();
            page = writer.getImportedPage(reader, pageNumber);
            rotation = reader.getPageRotation(pageNumber);
            if (rotation == 90 || rotation == 270) {
                cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader
                        .getPageSizeWithRotation(pageNumber).getHeight());
            } else {
                cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
            }

            document.close();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    /**
     * Get the text in the PDF file
     *
     * @param file
     * @return
     */
    public String getTextWithIText(String file) {
        try {
            PdfReader reader = new PdfReader(file);
            String content = "";
            PdfTextExtractor extractor = new PdfTextExtractor(reader);
            for (int i = 0; i < reader.getNumberOfPages(); i++) {
                content += extractor.getTextFromPage(i + 1);
            }
            return content;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

----------------------------------------------------------------------------------------------------------------------------------------


2009/8/4 1T3XT info <i...@1t3xt.info>

> Xavier Sudan wrote:
> > I think that this creation of PDF do something wrong and I need your
> > help to resolve it.
>
> The problem is that you don't help us help you. Reading your code, we
> have no clue. You MUST send us a small standalone example reproducing
> the problem. Otherwise, it's just as if you are asking a quiz question,
> a puzzle with missing pieces. We can't afford spending time guessing
> what you are doing wrong (unless you can provide code we can run on our
> machine without having to code anything ourselves).
> --
> This answer is provided by 1T3XT BVBA
> http://www.1t3xt.com/ - http://www.1t3xt.info
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus
> on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> iText-questions mailing list
> iText-questions@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
> Buy the iText book: http://www.1t3xt.com/docs/book.php
> Check the site with examples before you ask questions:
> http://www.1t3xt.info/examples/
> You can also search the keywords list:
> http://1t3xt.info/tutorials/keywords/
>
------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to