我试图修改我使用PDFBOX加载的. pdf。我正在使用RPG,我找到了从加载的. pdf访问页面的Java代码
PDPage page = (pdpage)pdDoc.getDocumentCatalog().getallPages().get(0)
我看到的另一个代码是
List pages = doc.getDocumentCatalog().getAllPages();
PDPage page = (PDPage)pages.get( i );
我遇到的问题是将代码翻译成RPG。
我现在的代码就是加载我的。pdf模板创建一个文档目录,创建我的列表并保存它(XLEnvVar是我获取类路径的地方)。
/free
XLSEnvVar();
// Load an existing PDF
myPath = jstring_new('/tmp/PDFtemplate.pdf');
File = File_new(myPath);
myDocument = PDdocument_load(File);
myCatalog = PDDocumentCatalog_new(myDocument);
myList = getAllPages(myCatalog);
// Done with this file, so save it
myFile = jstring_new('/tmp/PDFBox-demo.pdf');
PDDocument_save(myDocument:myFile);
// Close the document object in memory to free all allocated space
PDDocument_close(myDocument);
// Done
*INLR = *On;
/end-free
获取原型
D get...
D PR
D EXTPROC(*JAVA:
D 'java.util.List':
D 'get')
上面的原始帖子
我想我会发布我的PDFBOX程序,所以在互联网上还有另一个在RPG中使用PDFBOX的例子。这是一个完全有效的例子(它使用我公司的服务程序,SQL只是一个示例shell,所以如果有人要使用它,他们必须更改那些。但是我解释了这些服务程序在做什么,所以你可以使用你自己公司的服务程序或编写代码来做这些服务程序所做的事情)。可以随意拿走它并将其发布到其他地方,只需留下“编写者”块。希望这能帮助任何使用PDFBOX的人。
h/include QCpySrc,HSpecStd
//*****************************************************************
// Written By: Phillip Simmons Date: 05 20, 2013 *
// Special thanks to Spencer (co-worker) *
// and dhanuxp (code400 forums) and *
// http://wiki.midrange.com/index.php/PDFBox *
// for the intial example that I started with. *
// *
// Purpose: Load a pdf template, modify the template *
// then save the modified document. Using PDFBOX * / //*****************************************************************
/Include QCPYSRC,xCommandP
//Service program to return path on IFS
/Include QCPYSRC,GETIFSPTHP
//Service program to return document name
/Include QCPYSRC,GETIFSNAMP
//Service program for document to adopt authority of the
//folder above
/Include qcpysrc,ifsadoptp
//***************************************************************
// Data structure for SQL statement *
//***************************************************************
D Rcd DS qualified
D EMPLID LIKE(EMPLID)
D LNAME 90A VARYING
D ADDR1 LIKE(ADDR1)
D ADDR2 LIKE(ADDR2)
D CSZ 50A VARYING
D SDS
D ProgramName 1 10
//****************************************************************
// Prototypes for PDFBOX *
// Full documentation of PDFBox is available at *
// http://pdfbox.apache.org/index.html *
// Api Docs http://pdfbox.apache.org/apidocs/ *
//****************************************************************
/Copy QSYSINC/QRPGLESRC,JNI
*Classpath
D XLSEnvVar PR extpgm('XLSENVVAR')
*-----------------------------------------------------------------
* Global Constants
D DEFAULT_USERSPACE_UNIT_DPI...
D C 72
* Page sizes in mm
D PAGE_SIZE_A0 S Like(PDRectangle)
D PAGE_SIZE_A1 S Like(PDRectangle)
D PAGE_SIZE_A2 S Like(PDRectangle)
D PAGE_SIZE_A3 S Like(PDRectangle)
D PAGE_SIZE_A4 S Like(PDRectangle)
D PAGE_SIZE_A5 S Like(PDRectangle)
D PAGE_SIZE_A6 S Like(PDRectangle)
D PAGE_SIZE_A0_LANDSCAPE...
D S Like(PDRectangle)
D PAGE_SIZE_A1_LANDSCAPE...
D S Like(PDRectangle)
D PAGE_SIZE_A2_LANDSCAPE...
D S Like(PDRectangle)
D PAGE_SIZE_A3_LANDSCAPE...
D S Like(PDRectangle)
D PAGE_SIZE_A4_LANDSCAPE...
D S Like(PDRectangle)
D PAGE_SIZE_A5_LANDSCAPE...
D S Like(PDRectangle)
D PAGE_SIZE_A6_LANDSCAPE...
D S Like(PDRectangle)
D PAGE_WIDTH_A0 C 841
D PAGE_HEIGHT_A0 C 1189
D PAGE_WIDTH_A1 C 594
D PAGE_HEIGHT_A1 C 841
D PAGE_WIDTH_A2 C 420
D PAGE_HEIGHT_A2 C 594
D PAGE_WIDTH_A3 C 297
D PAGE_HEIGHT_A3 C 420
*210 orginal size
D PAGE_WIDTH_A4 C 216
*297 orginal size
D PAGE_HEIGHT_A4 C 279
D PAGE_WIDTH_A5 C 148
D PAGE_HEIGHT_A5 C 210
D PAGE_WIDTH_A6 C 105
D PAGE_HEIGHT_A6 C 148
*American pages slightly different size than ISO standard
*-----------------------------------------------------------------
* PDRectangle
D PDRectangleClass...
D C 'org.apache.pdfbox.pdmodel.common.PD-
D Rectangle'
D PDRectangle S O CLASS(*JAVA:PDRectangleClass)
* PDRectangle()
D PDRectangle_new...
D PR like(PDRectangle)
D EXTPROC(*JAVA:
D PDRectangleClass:
D *CONSTRUCTOR)
D width like(jfloat) Value
D height like(jfloat) Value
*-----------------------------------------------------------------
*PDDocumentCatalog
D DocumentCatalog...
D S O CLASS(*JAVA :
D PDDocumentCatalogClass)
D PDDocumentCatalogClass...
D C 'org.apache.pdfbox.pdmodel.PDDocumen-
D tCatalog'
D getDocumentCatalog...
D PR O CLASS(*JAVA :
D PDDocumentCatalogClass)
D EXTPROC(*JAVA :
D PDDocumentClass: 'getDocumen+
D tCatalog' )
*
D getAllPages PR O CLASS(*JAVA : 'java.util.List' )
D EXTPROC(*JAVA :
D PDDocumentCatalogClass : 'get+
D AllPages' )
*-----------------------------------------------------------------
*java.lang.String
D jStringClass...
D C 'java.lang.String'
D jstring_new PR like(jString)
D EXTPROC(*JAVA
D :jStringClass
D :*CONSTRUCTOR)
D create_from 1024A Varying const
*-----------------------------------------------------------------
*PDDocument
D PDDocumentClass...
D C 'org.apache.pdfbox.pdmodel.PDDocumen-
D t'
D PDDocument S O CLASS(*JAVA:PDDocumentClass)
* PDDocument()
D PDDocument_new...
D PR like(PDDocument)
D EXTPROC(*JAVA:
D PDDocumentClass:
D *CONSTRUCTOR)
* PDDocument.addPage()
D PDDocument_addPage...
D PR
D EXTPROC(*JAVA:
D PDDocumentClass:
D 'addPage')
D ppage like(PDPage)
*PDDocument.load()
D PDDocument_load...
D PR like(PDDocument)
D EXTPROC(*JAVA : PDDocumentClass:
D 'load' )
D STATIC
D path like(jstring)
D PDDocument_save...
D PR EXTPROC(*JAVA : PDDocumentClass:
D 'save' )
D savepath like(jstring)
*
D importPage PR O CLASS(*JAVA : 'org.apache.pdfbox.p+
D dmodel.PDPage' )
D EXTPROC(*JAVA : 'org.apache.pdfbox+
D .pdmodel.PDDocument' : 'importPage+
D ' )
D* Parameter prototype declaration for Java type: PDPage
D PDpage like(PDPage)
* PDDocument.close()
D PDDocument_close...
D PR
D EXTPROC(*JAVA:
D PDDocumentClass:
D 'close')
*-----------------------------------------------------------------
* Font Classes
D PDFontClass C 'org.apache.pdfbox.pdmodel.font.PDFo-
D nt'
D PDFont S O CLASS(*JAVA:PDFontClass)
*-----------------------------------------------------------------
* PDPage
D PDPageClass C 'org.apache.pdfbox.pdmodel.PDPage'
D PDPage S O CLASS(*JAVA:PDPageClass)
D PDPage_get PR O CLASS(*JAVA : 'java.lang.Object' )
D EXTPROC(*JAVA : 'java.util.List' :
D 'get' )
D indexNo 10I 0 VALUE
*-----------------------------------------------------------------
* PDType1Font
D PDType1FontClass...
D C 'org.apache.pdfbox.pdmodel.font.PDTy-
D pe1Font'
D PDType1Font S O CLASS(*JAVA:PDType1FontClass)
* PDType1Font()
D PDType1Font_new...
D PR like(PDType1Font)
D EXTPROC(*JAVA:
D PDType1FontClass:
D *CONSTRUCTOR)
D fontname like(jString) Options(*Omit)
* PDType1Font.getStandardFont
D PDType1Font_getStandardFont...
D PR like(PDType1Font)
D EXTPROC(*JAVA:
D PDType1FontClass:
D 'getStandardFont')
D STATIC
D fontname like(jString)
*-----------------------------------------------------------------
* PDPageContentStream
D PDPageContentStreamClass...
D C 'org.apache.pdfbox.pdmodel.edit.PDPa-
D geContentStream'
D PDPageContentStream...
D S O CLASS(*JAVA:
D PDPageContentStreamClass)
* PDPageContentStream()
D PDPageContentStream_new...
D PR like(PDPageContentStream)
D EXTPROC(*JAVA:
D PDPageContentStreamClass:
D *CONSTRUCTOR)
D pdocument like(PDDocument)
D ppage like(PDPage)
D appendcontent N value
D compress N value
* PDPageContentStream.beginText()
D PDPageContentStream_beginText...
D PR
D EXTPROC(*JAVA:
D PDPageContentStreamClass:
D 'beginText')
* PDPageContentStream.setFont()
D PDPageContentStream_setFont...
D PR
D EXTPROC(*JAVA:
D PDPageContentStreamClass:
D 'setFont')
D font like(PDFont)
D fontSize like(jfloat) Value
* PDPageContentStream.moveTextPositionByAmount()
D PDPageContentStream_moveTextPositionByAmount...
D PR
D EXTPROC(*JAVA:
D PDPageContentStreamClass:
D 'moveTextPositionByAmount')
D x like(jfloat) Value
D y like(jfloat) Value
* PDPageContentStream.drawString()
D PDPageContentStream_drawString...
D PR
D EXTPROC(*JAVA:
D PDPageContentStreamClass:
D 'drawString')
D text like(jstring)
* PDPageContentStream.endText()
D PDPageContentStream_endText...
D PR
D EXTPROC(*JAVA:
D PDPageContentStreamClass:
D 'endText')
* PDPageContentStream.close()
D PDPageContentStream_close...
D PR
D EXTPROC(*JAVA:
D PDPageContentStreamClass:
D 'close')
*-----------------------------------------------------------------
* java.awt.Color
D jColorClass...
D C 'java.awt.Color'
D jColor S O CLASS(*JAVA:
D jColorClass)
// new Color(int, int, int)
D jColor_new_fromIntRGB...
D PR like(jColor)
D EXTPROC(*JAVA
D :jColorClass
D :*CONSTRUCTOR)
D R like(jint) value
D G like(jint) value
D B like(jint) value
* PDPageContentStream.setNonStrokingColor(java.awt.Color color)
D PDPageContentStream_setNonStrokingColor...
D PR
D EXTPROC(*JAVA:
D PDPageContentStreamClass:
D 'setNonStrokingColor')
D color like(jColor)
*-----------------------------------------------------------------
* Utilities
D mmToUnits PR Like(jfloat)
D pmm Like(jfloat) Value
D createPageSizes...
D PR
*-----------------------------------------------------------------
*PDFBOX variables
D myFontName S like(jstring)
D myFont S like(PDType1Font)
D myPageContent S like(PDPageContentStream)
D myColor S like(jColor)
D sourceDoc S like(PDDocument)
D targetDoc S like(PDDocument)
D DocumentCat S like(DocumentCatalog)
D pdf_path1 S like(jstring)
D pdf_save S like(jstring)
D myPage S like(PDPage)
D newPage S like(PDPage)
D myAllPages S O CLASS(*JAVA : 'java.util.List' )
D importPageRet S like(PDPage)
*RPG variables
D DocName S 255A VARYING
D pageNumber S 10I 0
D Tdate S d
D Cdate S 10 VARYING
D tempsave S 150 VARYING
D tempload S 150 VARYING
D Directorys S 150 VARYING
D Directoryl S 150 VARYING
D CDirectory S 150 VARYING
D loadstring S 150 VARYING
D checkstring S 150 VARYING
D SqlStm S 1000A VARYING
D Q S 1A INZ(x'7D')
D Cmds S 1500A Varying
D Cmdl S 1500A Varying
D*--------------------------------------------------
D* Procedure name: addText
D* Purpose:
D* Returns:
D* Parameter: myText
D*--------------------------------------------------
D addText PR
D myText 5000A VARYING CONST
D indent 10I 0 CONST
D offset 10I 0 CONST
*
D myString S like(jstring)
/free
Tdate = %Date();
Cdate = %char(Tdate:*usa/);
//The page of the loaded pdf that I am going to want to retrieve
pageNumber = 0;
//Contains the classpath
XLSENVVAR();
//GETIFSPTH AND GETIFSNAM are service programs that my company uses
//to get the path for a document. Further down in PDF_path1(loadstring)
//you can pass a hardcoded string ex PDF_path1('/tmp/demodoc.pdf');
// Get the requested template file
DocName = %Trim(GETIFSPTH(ProgramName:1)) +
%Trim(GETIFSNAM(ProgramName:1));
//Directory the file will be moved into
Directoryl = %Trim(GETIFSPTH(ProgramName:2));
//getting path to load the pdf document
checkstring = %Trim(GETIFSPTH(ProgramName:3)) +
%Trim(GETIFSNAM(ProgramName:3));
//Check to see if template already exists if it does dlt it
//this is needed because later when I move the document to its final
//destination if the document to be moved already exists the move will
//fail. This will cause a modified template to be left behind and when
//the program is ran later the earlier cpy obj will fail and I will
//load an already modified templete.
CallP(E) xCommand('DEL OBJLNK('+Q+%Trim(checkstring)+Q+')');
//String to pass to xcommand()
Cmdl = 'CPY OBJ(' +Q + %trim(DocName) +Q + ')' +
' TODIR(' +Q + %trim(Directoryl) +Q + ')';
xcommand(Cmdl);
//Create a blank pdf document. This will be the page to be modified.
targetDoc = PDDocument_new();
//getting path to load the pdf document
loadstring = %Trim(GETIFSPTH(ProgramName:3)) +
%Trim(GETIFSNAM(ProgramName:3));
//The string that holds the path for where the pdf template is
pdf_path1 = jstring_new(loadstring);
//Load the pdf document
sourceDoc = PDDocument_load(pdf_path1);
//Get the document catalog
DocumentCat = getDocumentCatalog(sourceDoc);
//Get the list of pages
myAllPages = getAllPages(DocumentCat);
//Get the specific page I want
myPage = PDPage_get(myAllPages:pageNumber);
sqlstm = 'SELECT EMPLID,LNAME, ADDR1' +
' ADDR2, CSZ'
' from empmst';
Exec SQL
Prepare S1 From :SqlStm;
Exec SQL
Declare Rcd Cursor for S1;
Exec SQL
Open Rcd;
Exec SQL
Fetch Rcd into :Rcd;
Dow SQLCOD = 0;
//Import and copy to a new page. This is the page to be used
//in the content stream.
importpageret = importpage(targetdoc:myPage);
// Create a content stream so we can add content to the page
// Modifying an existing page so append is *ON
// Compression is *Off
myPageContent = PDPageContentStream_new(targetdoc:importpageret:
*ON:*Off);
//Adding text to preloaded pdf
addtext(Rcd.EMPLID :23 :240);
addtext(Rcd.LNAME :23 :231);
addtext(Rcd.ADDR1 :23 : 227);
addtext(Rcd.ADDR2 :23 : 223);
addtext(Rcd.CSZ :23 : 219);
addtext(Cdate :180 : 240);
EXEC SQL
Fetch Rcd into :Rcd;
ENDDO;
//Temparaly saving to /tmp then will move with mov command
tempsave = %Trim(GETIFSPTH(ProgramName:3)) +
%Trim(GETIFSNAM(ProgramName:3));
//The string that holds the path for where the pdf will be saved
pdf_save = jstring_new(tempsave);
//Done with the file, so save it
PDDocument_save(targetDoc : pdf_save);
// Close the document object in memory to free all allocated space
PDDocument_close(sourceDoc);
PDDocument_close(targetDoc);
//Create the directory that the file will be moved to
CDirectory = %Trim(GETIFSPTH(ProgramName:4))+
%char(%subdt(%date():*YEARS));
xcommand('CRTDIR DIR(' +Q + CDIRECTORY +Q +')');
//retrieve the path the file will be saved to
Directorys = %Trim(GETIFSPTH(ProgramName:4)) +
%char(%subdt(%date():*YEARS)) + '/' +
%Trim(GETIFSNAM(ProgramName:4));
//move the file to the correct directory
Cmds = 'MOV OBJ(' +Q + tempsave +Q + ')' +
' TOOBJ(' +Q + %trim(Directorys)+ Q + ')';
xcommand(Cmds);
//Adopt the auth of the folder
ifsadopt(Directorys);
Exec SQL
Close Rcd;
*Inlr = *On;
/end-free
* mmToUnits
P mmToUnits B
D Pi Like(jfloat)
D pmm value like(jfloat)
D funits S Like(jfloat)
/free
funits = 1 / (25.4) *
DEFAULT_USERSPACE_UNIT_DPI *
pmm;
return funits;
/end-free
P mmToUnits E
* creatPageSizes
P createPageSizes...
P B
D Pi
/free
PAGE_SIZE_A0 = PDRectangle_new(mmToUnits(PAGE_WIDTH_A0):
mmToUnits(PAGE_HEIGHT_A0));
PAGE_SIZE_A1 = PDRectangle_new(mmToUnits(PAGE_WIDTH_A1):
mmToUnits(PAGE_HEIGHT_A1));
PAGE_SIZE_A2 = PDRectangle_new(mmToUnits(PAGE_WIDTH_A2):
mmToUnits(PAGE_HEIGHT_A2));
PAGE_SIZE_A3 = PDRectangle_new(mmToUnits(PAGE_WIDTH_A3):
mmToUnits(PAGE_HEIGHT_A3));
PAGE_SIZE_A4 = PDRectangle_new(mmToUnits(PAGE_WIDTH_A4):
mmToUnits(PAGE_HEIGHT_A4));
PAGE_SIZE_A5 = PDRectangle_new(mmToUnits(PAGE_WIDTH_A5):
mmToUnits(PAGE_HEIGHT_A5));
PAGE_SIZE_A6 = PDRectangle_new(mmToUnits(PAGE_WIDTH_A6):
mmToUnits(PAGE_HEIGHT_A6));
PAGE_SIZE_A0_LANDSCAPE = PDRectangle_new(mmToUnits(PAGE_HEIGHT_A0):
mmToUnits(PAGE_WIDTH_A0));
PAGE_SIZE_A1_LANDSCAPE = PDRectangle_new(mmToUnits(PAGE_HEIGHT_A1):
mmToUnits(PAGE_WIDTH_A1));
PAGE_SIZE_A2_LANDSCAPE = PDRectangle_new(mmToUnits(PAGE_HEIGHT_A2):
mmToUnits(PAGE_WIDTH_A2));
PAGE_SIZE_A3_LANDSCAPE = PDRectangle_new(mmToUnits(PAGE_HEIGHT_A3):
mmToUnits(PAGE_WIDTH_A3));
PAGE_SIZE_A4_LANDSCAPE = PDRectangle_new(mmToUnits(PAGE_HEIGHT_A4):
mmToUnits(PAGE_WIDTH_A4));
PAGE_SIZE_A5_LANDSCAPE = PDRectangle_new(mmToUnits(PAGE_HEIGHT_A5):
mmToUnits(PAGE_WIDTH_A5));
PAGE_SIZE_A6_LANDSCAPE = PDRectangle_new(mmToUnits(PAGE_HEIGHT_A6):
mmToUnits(PAGE_WIDTH_A6));
return;
/end-free
P E
*
*
P*--------------------------------------------------
P* Procedure name: addText
P* Purpose: Write text to a PDPage
P* Returns:
P* Parameter: myText
P*--------------------------------------------------
P addText B
D addText PI
D myText 5000A VARYING CONST
D indent 10I 0 CONST
D offset 10I 0 CONST
*
D myString S like(jstring)
/FREE
// Set font type
myFontName = jstring_new('Times-Roman');
myFont = PDType1Font_getStandardFont(myFontName);
PDPageContentStream_setFont(myPageContent:myFont:10);
// Set the color of the font to add
// This isn't needed if writing to a blank page but since
// adding text to a loaded page need to set the color of the text.
myColor = jColor_new_fromIntRGB(0:0:0);
PDPageContentStream_setNonStrokingColor(myPageContent:myColor);
// Prepare to write text to the content stream
PDPageContentStream_beginText(myPageContent);
// Move the cursor possition,indent is x mm from left of page
// offset x mm from bottom of page.
PDPageContentStream_moveTextPositionByAmount(myPageContent:
mmtounits(indent):mmtounits(offset));
// Create the text string we want to write
myString = jstring_new(myText);
// Write the string to the content stream
PDPageContentStream_drawString(myPageContent:myString);
// We are done writing text to this content stream
PDPageContentStream_endText(myPageContent);
//We are done with this content stream
PDPageContentStream_close(myPageContent);
/END-FREE
P addText E
我想我会在阅读代码后添加一些注释。我每年都要做一个新文件夹。这就是crtdir正在做的事情。如果目录已经存在,那么已经创建的目录中的内容是安全的,crtdir不会覆盖该目录。我将模板从一个位置加载到/tmp,然后在再次移动之前保存到/tmp的原因是,当我使用PDFBOX时,jstring_new(“”)的最大值为30个字符(包括/和空格)。所以myString=jstring_new('/tmp/2013/myReportNameThatIsToLong.pdf')这个字符串有39个字符长,这个字符串将被截断。奥龙。当我制作字符串时,pdf将丢失。我可以用rpg代码生成一个字符串,并将该字符串传递给jString_new(),长度超过30个字符,它就可以工作了。
你会想要你的清单。get()返回org。阿帕奇。pdfbox。pdmodel。PDPage
RPG是强类型的,所以我们不能像Java那样强制转换。相反,我们创建原型以返回我们想要操纵的对象。按照这些思路,最终可能会使用get()检索几种不同类型的列表元素。RPG也不做重载。我要做的是为我要处理的每个对象创建一个单独的get()原型。我有一个getPage()和一个getThread()等等。每一个都是“java”。util。List:get'但返回不同的对象类型。
点击www.middrange查看RPG列表。com是另一个帮助RPG的好来源。
我正在使用PDFBox(v2.0.13)合并PDF文件 这些文件是 合并后的文件是 我可以去掉使第2页成为第1页的空白吗? 关于合并代码,我使用pdfbox github示例代码:https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PDFMergerEx
我正在使用PDFbox下载PDF。我想在中间添加一些新页面。 此代码在PDF结尾处插入新页面。如何在另一个位置插入页面?
我使用的代码是:https://www.tutorialspoint.com/pdfbox/pdfbox_inserting_image.htm 帮助我将图像添加到现有PDF。问题是它创建的文件是一个空白页,上面只有图像。 这是我的代码: 据我所知,我所做的应该是有效的,我没有任何错误,那么是什么造成的呢?
我正在尝试使用Apache PDFBox库以编程方式创建PDF文档。PDPageContentStream类包含写入文本、绘制直线、贝塞尔曲线和矩形的方法。但我找不到一种方法来画一个简单的实心圆。有没有办法用这个库来绘制它?如果没有,您能否建议一个免费的Java库,它提供灵活的API以编程方式创建PDF文档?提前谢谢。
问题内容: 我的设计师认为在不同页面之间创建过渡是一个好主意。本质上,只有内容部分会重新加载(页眉和页脚保持不变),并且只有内容div应该具有过渡效果(淡入淡出或某种形式)。创造这种效果并不是真正的问题,让Google(分析)感到高兴的是… 我不喜欢的解决方案以及原因; 仅使用ajax加载内容div:谷歌将看不到任何内容,这意味着将永远找不到该网站,或者仅显示由ajax检索的部分,而这些部分根本不