Skip to content

Commit 3cd6064

Browse files
Added "Add Table In Existing PDF Document" example.
1 parent 00dadef commit 3cd6064

File tree

9 files changed

+157
-21
lines changed

9 files changed

+157
-21
lines changed

Examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<dependency>
1414
<groupId>com.aspose</groupId>
1515
<artifactId>aspose-pdf</artifactId>
16-
<version>10.4.0</version>
16+
<version>11.8.0</version>
1717
</dependency>
1818
<dependency>
1919
<groupId>javax.media.jai</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.aspose.pdf.examples.AsposePdfExamples.DocumentConversion;
2+
3+
import com.aspose.pdf.Document;
4+
import com.aspose.pdf.HtmlSaveOptions;
5+
import com.aspose.pdf.examples.Utils;
6+
7+
public class PDFToHTMLRenderPDFDataLayersAsSeparateHTMLLayerElement {
8+
9+
public static void main(String[] args) {
10+
// The path to the resource directory.
11+
String dataDir = Utils.getSharedDataDir(PDFToHTMLRenderPDFDataLayersAsSeparateHTMLLayerElement.class) + "PDFToHTML/";
12+
// Open the PDF file
13+
Document doc = new Document(dataDir + "input.pdf");
14+
// Instantiate HTML SaveOptions object
15+
HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
16+
// Specify to render PDF document layers separately in output HTML
17+
htmlOptions.setConvertMarkedContentToLayers(true);
18+
// Save the document
19+
doc.save(dataDir + "output.html", htmlOptions);
20+
}
21+
22+
}

Examples/src/main/java/com/aspose/pdf/examples/AsposePdfExamples/Images/ConvertPDFPagesToTIFFImage.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,43 @@ public static void main(String[] args) throws IOException {
1717
}
1818

1919
public static void convertAllPDFPagesToTIFFImages() throws IOException {
20+
// Open document
21+
Document pdfDocument = new Document("input.pdf");
22+
23+
// Create stream object to save the output image
24+
java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image.tiff");
25+
26+
// Create Resolution object
27+
Resolution resolution = new Resolution(300);
28+
29+
// instantiate TiffSettings object
30+
TiffSettings tiffSettings = new TiffSettings();
31+
// set the compression of resultant TIFF image
32+
tiffSettings.setCompression(CompressionType.CCITT4);
33+
// set the color depth for resultant image
34+
tiffSettings.setDepth(ColorDepth.Format8bpp);
35+
// skip blank pages while rendering PDF to TIFF
36+
tiffSettings.setSkipBlankPages(true);
37+
38+
// Create TiffDevice object with particular resolution
39+
TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);
2040
// Convert a all pages of PDF file to TIFF format
2141
tiffDevice.process(pdfDocument, imageStream);
42+
43+
// Close the stream
44+
imageStream.close();
2245
}
2346

2447
public static void convertOnePageToTIFF() throws IOException {
2548
// Open document
2649
Document pdfDocument = new Document("input.pdf");
50+
2751
// Create stream object to save the output image
2852
java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image.tiff");
53+
2954
// Create Resolution object
3055
Resolution resolution = new Resolution(300);
56+
3157
// instantiate TiffSettings object
3258
TiffSettings tiffSettings = new TiffSettings();
3359
// set the compression of resultant TIFF image
@@ -36,10 +62,12 @@ public static void convertOnePageToTIFF() throws IOException {
3662
tiffSettings.setDepth(ColorDepth.Format8bpp);
3763
// skip blank pages while rendering PDF to TIFF
3864
tiffSettings.setSkipBlankPages(true);
65+
3966
// Create TiffDevice object with particular resolution
4067
TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);
4168
// Convert a particular page (Page 1) and save the image to stream
4269
tiffDevice.process(pdfDocument, 1, 1, imageStream);
70+
4371
// Close the stream
4472
imageStream.close();
4573
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.aspose.pdf.examples.AsposePdfExamples.Tables;
2+
3+
import com.aspose.pdf.BorderInfo;
4+
import com.aspose.pdf.BorderSide;
5+
import com.aspose.pdf.Color;
6+
import com.aspose.pdf.ColumnAdjustment;
7+
import com.aspose.pdf.Document;
8+
import com.aspose.pdf.Page;
9+
import com.aspose.pdf.Row;
10+
import com.aspose.pdf.Table;
11+
import com.aspose.pdf.examples.Utils;
12+
13+
public class AddTableInExistingPDFDocument {
14+
15+
public static void main(String[] args) {
16+
// The path to the resource directory.
17+
String dataDir = Utils.getSharedDataDir(AddTableInExistingPDFDocument.class) + "AsposePdfExamples/Tables/";
18+
19+
addTableInExistingPDFDocument(dataDir);
20+
setAutoFitToWindowPropertyInColumnAdjustmentTypeEnumeration(dataDir);
21+
}
22+
23+
public static void addTableInExistingPDFDocument(String dataDir) {
24+
// Load source PDF document
25+
Document doc = new Document(dataDir + "input.pdf");
26+
// Initializes a new instance of the Table
27+
Table table = new Table();
28+
// Set the table border color as LightGray
29+
table.setBorder(new BorderInfo(BorderSide.All, .5f, Color.getLightGray()));
30+
// set the border for table cells
31+
table.setDefaultCellBorder(new BorderInfo(BorderSide.All, .5f, Color.getLightGray()));
32+
// create a loop to add 10 rows
33+
for (int row_count = 1; row_count < 10; row_count++) {
34+
// add row to table
35+
Row row = table.getRows().add();
36+
// add table cells
37+
row.getCells().add("Column (" + row_count + ", 1)");
38+
row.getCells().add("Column (" + row_count + ", 2)");
39+
row.getCells().add("Column (" + row_count + ", 3)");
40+
}
41+
// Add table object to first page of input document
42+
doc.getPages().get_Item(1).getParagraphs().add(table);
43+
// Save updated document containing table object
44+
doc.save(dataDir + "document_with_table.pdf");
45+
}
46+
47+
public static void setAutoFitToWindowPropertyInColumnAdjustmentTypeEnumeration(String dataDir) {
48+
//Instantiate the PDF object by calling its empty constructor
49+
Document doc = new Document();
50+
//Create the section in the PDF object
51+
Page page = doc.getPages().add();
52+
53+
//Instantiate a table object
54+
Table tab = new Table();
55+
//Add the table in paragraphs collection of the desired section
56+
page.getParagraphs().add(tab);
57+
58+
//Set with column widths of the table
59+
tab.setColumnWidths("50 50 50");
60+
tab.setColumnAdjustment(ColumnAdjustment.AutoFitToWindow);
61+
62+
//Set default cell border using BorderInfo object
63+
tab.setDefaultCellBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.1F));
64+
65+
//Set table border using another customized BorderInfo object
66+
tab.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 1F));
67+
//Create MarginInfo object and set its left, bottom, right and top margins
68+
com.aspose.pdf.MarginInfo margin = new com.aspose.pdf.MarginInfo();
69+
margin.setTop(5f);
70+
margin.setLeft(5f);
71+
margin.setRight(5f);
72+
margin.setBottom(5f);
73+
74+
//Set the default cell padding to the MarginInfo object
75+
tab.setDefaultCellPadding(margin);
76+
77+
//Create rows in the table and then cells in the rows
78+
com.aspose.pdf.Row row1 = tab.getRows().add();
79+
row1.getCells().add("col1");
80+
row1.getCells().add("col2");
81+
row1.getCells().add("col3");
82+
com.aspose.pdf.Row row2 = tab.getRows().add();
83+
row2.getCells().add("item1");
84+
row2.getCells().add("item2");
85+
row2.getCells().add("item3");
86+
87+
//Save the PDF
88+
doc.save(dataDir + "ResultantFile.pdf");
89+
}
90+
91+
}

Examples/src/main/java/com/aspose/pdf/examples/AsposePdfExamples/Tables/ManipulateTablesInExistingPDF.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
import com.aspose.pdf.TextFragment;
77

88
public class ManipulateTablesInExistingPDF {
9-
9+
10+
/*
11+
* Update contents in particular table cell
12+
*/
1013
public static void main(String[] args) {
11-
generalCode();
12-
futureEnhancements();
13-
}
14-
15-
public static void generalCode() {
1614
// load existing PDF file
1715
Document pdfDocument = new Document("table.pdf");
1816
// Create TableAbsorber object to find tables
@@ -27,14 +25,4 @@ public static void generalCode() {
2725
// save updated document
2826
pdfDocument.save("Table_Manipulated.pdf");
2927
}
30-
31-
public static void futureEnhancements() {
32-
for (Row row : (Iterable<Row>) table.getRows()) {
33-
TextFragment updatedfragment = (TextFragment) row.getCells().get_Item(1).getParagraphs().get_Item(1);
34-
String text;
35-
if (updatedfragment != null)
36-
text = updatedfragment.getText();
37-
}
38-
}
39-
4028
}

Examples/src/main/java/com/aspose/pdf/examples/AsposePdfLegacy/Images/LoadImageFromLocalDisk.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public static void main(String[] args) {
1717
// Add image object into the Paragraphs collection of the section
1818
sec1.getParagraphs().add(img1);
1919
// Specify the Image file type as JPEG
20-
img1.getImageInfo().setImageFileType(ImageFileType.Jpeg);
20+
img1.getImageInfo().setImageFileType(ImageFileType.Png);
2121
// Set the path of image file
22-
img1.getImageInfo().setFile("Apple.jpg");
22+
img1.getImageInfo().setFile("apple.png");
2323
// Set the path of image file
2424
img1.getImageInfo().setTitle("JPEG image");
2525
// Save the Pdf

Examples/src/main/java/com/aspose/pdf/examples/Utils.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.aspose.pdf.examples;
22

3-
import com.aspose.pdf.License;
4-
53
import java.io.File;
64

75
public class Utils {
@@ -21,4 +19,13 @@ public static String getDataDir(Class c) {
2119
System.out.println("Using data directory: " + dir.toString());
2220
return dir.toString() + File.separator;
2321
}
22+
23+
public static String getSharedDataDir(Class c) {
24+
File dir = new File(System.getProperty("user.dir"));
25+
dir = new File(dir, "src");
26+
dir = new File(dir, "main");
27+
dir = new File(dir, "resources");
28+
29+
return dir.toString() + File.separator;
30+
}
2431
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)