2011-10-20 18 views
0

PDFファイルから特定のページを編集する必要があるため、そのページを別のPDFファイルとして保存する必要があります。私は仕事を成功裏に完了しました。入力ファイルはレターサイズのページです。しかし、今私が必要とするのは、編集したページをA4サイズで保存することです。誰でも助けてくれますか?私はその反応を待っている。編集したPDFを保存するitextを使用してA4サイズのページ

以下は私のコードです。

import java.awt.Color; 

import java.io.FileOutputStream; 
import java.io.IOException; 

import com.lowagie.text.Chunk; 
import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Element; 
import com.lowagie.text.Font; 
import com.lowagie.text.Paragraph; 
import com.lowagie.text.pdf.ColumnText; 
import com.lowagie.text.pdf.PdfContentByte; 
import com.lowagie.text.pdf.PdfReader; 
import com.lowagie.text.pdf.PdfStamper; 

public class pdfEdit { 

    private static String INPUTFILE = "./pdf/eng.pdf"; 
    private static String OUTPUTFILE = "./pdf/output.pdf"; 
    private static String footerRight = "Web content and services"; 
    private static String footerLeft = "All Rigths Reserved"; 
    private static Document document; 

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

     document = new Document();  
     document.open(); 

     PdfReader reader = new PdfReader(INPUTFILE); 
     PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(OUTPUTFILE));  

     int editingPage = 3; 

     PdfContentByte cb = stamper.getOverContent(editingPage); 
     cb.rectangle(10, 10, 550, 30); 
     cb.setRGBColorFill(255, 255, 255); 
     cb.fill(); 

     PdfContentByte cByte = stamper.getOverContent(editingPage); 
     editFooterText(cByte); 
     reader.selectPages(Integer.toString(editingPage)); 

     stamper.close(); 
     document.close(); 
    } 


    private static void editFooterText(PdfContentByte cByte) throws DocumentException { 

     Font footerFont = new Font(Font.HELVETICA, 5f, Font.NORMAL, Color.BLACK); 

     ColumnText cTextLeft = new ColumnText(cByte); 
     Paragraph leftPara = new Paragraph(); 
     cTextLeft.setAlignment(Element.ALIGN_LEFT); 
     cTextLeft.setSimpleColumn(document.left(), 10, 500, document.bottom()); 
     Chunk strFooterLeft = new Chunk(footerLeft, footerFont); 
     leftPara.add(strFooterLeft); 
     cTextLeft.addElement(leftPara); 
     cTextLeft.go(); 

     ColumnText cTextRight = new ColumnText(cByte); 
     cTextRight.setSimpleColumn(document.left(), 10, 430, document.bottom()); 
     Paragraph Rightpara = new Paragraph(); 
     Chunk strfooterRight = new Chunk(footerRight, footerFont); 
     Rightpara.setAlignment(Element.ALIGN_RIGHT); 
     Rightpara.add(strfooterRight); 
     cTextRight.addElement(Rightpara); 
     cTextRight.go(); 
    }  
} 
+0

こんにちは友人..私を助けてください。 – 1355

+0

まだ私は返信を期待しています – 1355

答えて

0
private static void createPDFFile() throws FileNotFoundException { 
     OutputStream outputStream = new FileOutputStream("./A4SizePdf.pdf"); 
     Document document = new Document(PageSize.A4, 40, 40, 40, 40); 
     try { 

      PdfWriter writer = PdfWriter.getInstance(document, outputStream); 
      PdfReader reader = new PdfReader(./InputPdf.pdf); 
      reader.setViewerPreferences(editedPageNo); 

      PdfImportedPage page = writer.getImportedPage(reader, 1); 
      document.open();     

      PdfContentByte cb = writer.getDirectContent(); 
      cb.addTemplate(page, 1.4f, 0, 0, 1.19f, -13, 7); 
      document.close(); 
      outputStream.close();   
      setFooter(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (document.isOpen()) 
       document.close(); 
      try { 
       if (outputStream != null) 
        outputStream.close(); 
      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } 
     } 

    } 
1

試してみてください。reader.getPageN(Integer.toString(editingPage)).put(PdfName.MEDIABOX, new PdfRectangle(612,842));

+0

あなたの応答に非常に感謝します。しかし、それは動作していません – 1355

+0

あなたはより具体的になることができますか?間違いはありますか? –

+0

エラーはありません。しかし、pdfは作成されていません – 1355

関連する問題