Javaの標準またはカスタム用紙サイズに変更/リセットPDFページのサイズ
19176 ワード
ここでは、どのように変更/リセットPDFファイルのページサイズをJavaで無料でスパイスを使用してプログラムを説明します.JavaライブラリのPDF.
自由尖塔.JavaのPDFは、PDF文書内のページのサイズを変更するには、新しいドキュメントを作成することによって、希望の用紙サイズの新しいページを作成し、元のPDFドキュメントからテンプレートを作成し、後で新しい文書の新しいページにテンプレートを描画します.このプロセスは、元のPDFに存在するテキスト、画像、その他のすべての要素を保持します.
内容概要は、標準的な紙サイズ にPDFページサイズを変えますは、カスタム紙サイズ にPDFページサイズを変えますは、元のページサイズ を縮小することによって、PDFページサイズを変えます
次の例を実行する前に、JavaプロジェクトでFree Spire.PDF for Java libraryを含む依存関係を追加する必要があります.
PDFページサイズを標準の紙サイズに変えてください
自由尖塔.JavaのPDFは、A 0、A 1、A 2、A 3、A 4、B 0、B 1、B 2、B 3、B 4などの標準的な用紙サイズをサポートしています.サポートされている用紙サイズの完全な型は、pdfpagesize enumにあります.
次の例では、PDFドキュメントのページサイズをA 1に変更する方法を参照してください.
カスタムペーパーサイズにPDFページサイズを変更する
無料の尖塔で使用されるユニット.PDFライブラリはpt(point)です.カスタム用紙サイズにPDFページサイズをリセットするには、単位から、ミリメートル/センチメートルまでの単位を変換する必要があります.
PDFドキュメントのページサイズを6.5 * 8.5インチのカスタム用紙サイズに変更する方法を次の例に示します.
元のページサイズを縮小してPDFページのサイズを変更する
元のPDFドキュメントのページサイズを縮小することによってPDFページサイズを変更する方法を次の例に示します.
自由尖塔.JavaのPDFは、PDF文書内のページのサイズを変更するには、新しいドキュメントを作成することによって、希望の用紙サイズの新しいページを作成し、元のPDFドキュメントからテンプレートを作成し、後で新しい文書の新しいページにテンプレートを描画します.このプロセスは、元のPDFに存在するテキスト、画像、その他のすべての要素を保持します.
内容概要
次の例を実行する前に、JavaプロジェクトでFree Spire.PDF for Java libraryを含む依存関係を追加する必要があります.
PDFページサイズを標準の紙サイズに変えてください
自由尖塔.JavaのPDFは、A 0、A 1、A 2、A 3、A 4、B 0、B 1、B 2、B 3、B 4などの標準的な用紙サイズをサポートしています.サポートされている用紙サイズの完全な型は、pdfpagesize enumにあります.
次の例では、PDFドキュメントのページサイズをA 1に変更する方法を参照してください.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;
import java.awt.geom.Point2D;
public class ResetToStandardPageSize {
public static void main(String []args){
//Create a new PDF document
PdfDocument newPdf = new PdfDocument();
//Set page size as A1
newPdf.getPageSettings().setSize(PdfPageSize.A1);
//Remove page margins
newPdf.getPageSettings().setMargins(new PdfMargins(0));
//Load the sample PDF document
PdfDocument pdf = new PdfDocument("Sample.pdf");
//Iterate through the pages in the sample PDF
for(int i = 0; i< pdf.getPages().getCount(); i++)
{
//Add pages to the new PDF
PdfPageBase newPage = newPdf.getPages().add();
//Set text layout as 1 page, if not set the content will not scale to fit page size
PdfTextLayout layout = new PdfTextLayout();
layout.setLayout(PdfLayoutType.One_Page);
//Create templates for the pages in sample PDF and draw templates to the pages of the new PDF
pdf.getPages().get(i).createTemplate().draw(newPage, new Point2D.Float(0,0), layout);
}
//Save the result document
newPdf.saveToFile("ChangePageSizeToA1.pdf");
}
}
出力PDF :カスタムペーパーサイズにPDFページサイズを変更する
無料の尖塔で使用されるユニット.PDFライブラリはpt(point)です.カスタム用紙サイズにPDFページサイズをリセットするには、単位から、ミリメートル/センチメートルまでの単位を変換する必要があります.
PDFドキュメントのページサイズを6.5 * 8.5インチのカスタム用紙サイズに変更する方法を次の例に示します.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
public class ResetToCustomPageSize {
public static void main(String []args){
//Create a new PDF
PdfDocument newPdf = new PdfDocument();
//Convert inches to points
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
float width = unitCvtr.convertUnits(6.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
float height = unitCvtr.convertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
//Specify custom size
Dimension2D customSize = new Dimension((int)width, (int)height);
//Set page size as the custom size
newPdf.getPageSettings().setSize(customSize);
//Remove page margins
newPdf.getPageSettings().setMargins(new PdfMargins(0));
//Load the sample PDF
PdfDocument pdf = new PdfDocument("Sample.pdf");
//Iterate through the pages in the sampe PDF
for(int i = 0; i< pdf.getPages().getCount(); i++)
{
//Add pages to the new PDF
PdfPageBase newPage = newPdf.getPages().add();
//Set text layout as 1 page, if not set the content will not scale to fit page size
PdfTextLayout layout = new PdfTextLayout();
layout.setLayout(PdfLayoutType.One_Page);
//Create templates for the pages in sample PDF and draw templates to the pages of the new PDF
pdf.getPages().get(i).createTemplate().draw(newPage, new Point2D.Float(0,0), layout);
}
//Save the result document
newPdf.saveToFile("ChangePageSizetoCustomPaperSize.pdf");
}
}
出力PDF :元のページサイズを縮小してPDFページのサイズを変更する
元のPDFドキュメントのページサイズを縮小することによってPDFページサイズを変更する方法を次の例に示します.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
public class ResetPageSize {
public static void main(String []args){
//Load the sample PDF
PdfDocument pdf = new PdfDocument("Sample.pdf");
//Sepecify new size by scaling down the orginal page size
float width = pdf.getPageSettings().getWidth() * 0.8f;
float height = pdf.getPageSettings().getHeight() * 0.8f;
Dimension2D newSize = new Dimension((int)width, (int)height);
//Create a new PDF
PdfDocument newPdf = new PdfDocument();
//Set page size as the new size
newPdf.getPageSettings().setSize(newSize);
//Remove page margins
newPdf.getPageSettings().setMargins(new PdfMargins(0));
//Iterate through the pages in the sampe PDF
for(int i = 0; i< pdf.getPages().getCount(); i++)
{
//Add pages to the new PDF
PdfPageBase newPage = newPdf.getPages().add();
//Set text layout as 1 page, if not set the content will not scale to fit page size
PdfTextLayout layout = new PdfTextLayout();
layout.setLayout(PdfLayoutType.One_Page);
//Create templates for the pages in sample PDF and draw templates to the pages of the new PDF
pdf.getPages().get(i).createTemplate().draw(newPage, new Point2D.Float(0,0), layout);
}
//Save the result document
newPdf.saveToFile("ScaleDownPageSize.pdf");
}
}
出力PDF :Reference
この問題について(Javaの標準またはカスタム用紙サイズに変更/リセットPDFページのサイズ), 我々は、より多くの情報をここで見つけました https://dev.to/eiceblue/change-reset-pdf-page-size-to-standard-or-custom-paper-size-in-java-3619テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol