Skip to content

Commit dbbc9d4

Browse files
Aspose.PDF for Go via C++ 25.4
1 parent b968a98 commit dbbc9d4

19 files changed

+730
-5
lines changed

asposepdf_test.go

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"reflect"
10+
"strings"
1011
"testing"
1112
)
1213

@@ -117,3 +118,258 @@ func TestStats(t *testing.T) {
117118
page_is_blank, _ := pdf.PageIsBlank(1)
118119
assert_eq(t, page_is_blank, false)
119120
}
121+
122+
func TestAppend(t *testing.T) {
123+
// Create the first PDF-document
124+
pdf1, err := New()
125+
if err != nil {
126+
t.Errorf("New(): %v", err)
127+
}
128+
defer pdf1.Close()
129+
130+
// Add two empty pages to the first PDF-document
131+
for i := 0; i < 2; i++ {
132+
_ = pdf1.PageAdd()
133+
}
134+
135+
// Check the page count in the first PDF-document (should be 2 pages)
136+
page_count1, _ := pdf1.PageCount()
137+
assert_eq(t, page_count1, int32(2))
138+
139+
// Create the second PDF-document
140+
pdf2, err := New()
141+
if err != nil {
142+
t.Errorf("New(): %v", err)
143+
}
144+
defer pdf2.Close()
145+
146+
// Add two empty pages to the second PDF-document
147+
for i := 0; i < 2; i++ {
148+
_ = pdf2.PageAdd()
149+
}
150+
151+
// Check the page count in the second PDF-document (should be 2 pages)
152+
page_count2, _ := pdf2.PageCount()
153+
assert_eq(t, page_count2, int32(2))
154+
155+
// Append pages from the second PDF-document to the first
156+
err = pdf1.Append(pdf2)
157+
if err != nil {
158+
t.Errorf("Append(): %v", err)
159+
}
160+
161+
// Check the page count in the first PDF-document after appending (should be 4 pages)
162+
page_count1_after_append, _ := pdf1.PageCount()
163+
assert_eq(t, page_count1_after_append, int32(4))
164+
}
165+
166+
func TestExtractText(t *testing.T) {
167+
// Create a new document
168+
doc, err := New()
169+
if err != nil {
170+
t.Fatalf("New(): %v", err)
171+
}
172+
defer doc.Close()
173+
174+
// Add a page and some text
175+
if err := doc.PageAdd(); err != nil {
176+
t.Fatalf("PageAdd(): %v", err)
177+
}
178+
expectedText := "This is a test text for extraction"
179+
if err := doc.PageAddText(1, expectedText); err != nil {
180+
t.Fatalf("PageAddText(): %v", err)
181+
}
182+
183+
// Save to apply changes
184+
if err := doc.Save(); err != nil {
185+
t.Fatalf("Save(): %v", err)
186+
}
187+
188+
// Extract text
189+
extracted, err := doc.ExtractText()
190+
if err != nil {
191+
t.Fatalf("ExtractText(): %v", err)
192+
}
193+
194+
// Validate that extracted text contains the inserted text
195+
if len(extracted) == 0 {
196+
t.Errorf("Extracted text is empty")
197+
}
198+
if !strings.Contains(extracted, "test text") {
199+
t.Errorf("Extracted text does not contain expected content.\nGot: %s", extracted)
200+
}
201+
}
202+
203+
func TestConvertFromPDF(t *testing.T) {
204+
type conversion struct {
205+
name string
206+
fn func(doc *Document, path string) error
207+
}
208+
209+
conversions := []conversion{
210+
{"SaveDocX", func(doc *Document, path string) error { return doc.SaveDocX(path) }},
211+
{"SaveDoc", func(doc *Document, path string) error { return doc.SaveDoc(path) }},
212+
{"SaveXlsX", func(doc *Document, path string) error { return doc.SaveXlsX(path) }},
213+
{"SaveTxt", func(doc *Document, path string) error { return doc.SaveTxt(path) }},
214+
{"SavePptX", func(doc *Document, path string) error { return doc.SavePptX(path) }},
215+
{"SaveXps", func(doc *Document, path string) error { return doc.SaveXps(path) }},
216+
{"SaveTeX", func(doc *Document, path string) error { return doc.SaveTeX(path) }},
217+
{"SaveEpub", func(doc *Document, path string) error { return doc.SaveEpub(path) }},
218+
{"SaveBooklet", func(doc *Document, path string) error { return doc.SaveBooklet(path) }},
219+
{"SaveNUp", func(doc *Document, path string) error { return doc.SaveNUp(path, 2, 2) }},
220+
{"SaveMarkdown", func(doc *Document, path string) error { return doc.SaveMarkdown(path) }},
221+
{"ExportFdf", func(doc *Document, path string) error { return doc.ExportFdf(path) }},
222+
{"ExportXfdf", func(doc *Document, path string) error { return doc.ExportXfdf(path) }},
223+
{"ExportXml", func(doc *Document, path string) error { return doc.ExportXml(path) }},
224+
{"PageToJpg", func(doc *Document, path string) error { return doc.PageToJpg(1, 150, path) }},
225+
{"PageToPng", func(doc *Document, path string) error { return doc.PageToPng(1, 150, path) }},
226+
{"PageToBmp", func(doc *Document, path string) error { return doc.PageToBmp(1, 150, path) }},
227+
{"PageToTiff", func(doc *Document, path string) error { return doc.PageToTiff(1, 150, path) }},
228+
{"PageToSvg", func(doc *Document, path string) error { return doc.PageToSvg(1, path) }},
229+
{"PageToPdf", func(doc *Document, path string) error { return doc.PageToPdf(1, path) }},
230+
{"PageToDICOM", func(doc *Document, path string) error { return doc.PageToDICOM(1, 150, path) }},
231+
}
232+
233+
for _, conv := range conversions {
234+
t.Run(conv.name, func(t *testing.T) {
235+
// Create new document
236+
doc, err := New()
237+
if err != nil {
238+
t.Fatalf("New(): %v", err)
239+
}
240+
defer doc.Close()
241+
242+
// Add one page with text
243+
if err := doc.PageAdd(); err != nil {
244+
t.Fatalf("PageAdd(): %v", err)
245+
}
246+
if err := doc.PageAddText(1, fmt.Sprintf("Test conversion for %s", conv.name)); err != nil {
247+
t.Fatalf("PageAddText(): %v", err)
248+
}
249+
250+
// Save document before conversion
251+
if err := doc.Save(); err != nil {
252+
t.Fatalf("Save(): %v", err)
253+
}
254+
255+
// Prepare output path
256+
outputPath := fmt.Sprintf("%s/output", t.TempDir())
257+
258+
// Call conversion function
259+
if err := conv.fn(doc, outputPath); err != nil {
260+
t.Errorf("%s failed: %v", conv.name, err)
261+
}
262+
263+
// Check file was created and is non-zero
264+
info, err := os.Stat(outputPath)
265+
if err != nil {
266+
t.Errorf("Stat(%s): %v", outputPath, err)
267+
} else {
268+
assert_ne(t, int64(0), info.Size())
269+
}
270+
})
271+
}
272+
}
273+
274+
func TestOrganize(t *testing.T) {
275+
type organizeFunction struct {
276+
name string
277+
fn func(doc *Document) error
278+
}
279+
280+
organizeFunctions := []organizeFunction{
281+
{"Optimize", (*Document).Optimize},
282+
{"OptimizeResource", (*Document).OptimizeResource},
283+
{"Grayscale", (*Document).Grayscale},
284+
{"Rotate", func(doc *Document) error { return doc.Rotate(RotationOn270) }},
285+
{"SetBackground", func(doc *Document) error { return doc.SetBackground(255, 255, 200) }},
286+
{"ReplaceText", func(doc *Document) error {
287+
_ = doc.PageAddText(1, "Hello World")
288+
return doc.ReplaceText("Hello", "Hi")
289+
}},
290+
{"AddPageNum", (*Document).AddPageNum},
291+
{"AddTextHeader", func(doc *Document) error { return doc.AddTextHeader("Header") }},
292+
{"AddTextFooter", func(doc *Document) error { return doc.AddTextFooter("Footer") }},
293+
{"Flatten", (*Document).Flatten},
294+
{"RemoveAnnotations", (*Document).RemoveAnnotations},
295+
{"RemoveAttachments", (*Document).RemoveAttachments},
296+
{"RemoveBlankPages", (*Document).RemoveBlankPages},
297+
{"RemoveBookmarks", (*Document).RemoveBookmarks},
298+
{"RemoveHiddenText", (*Document).RemoveHiddenText},
299+
{"RemoveImages", (*Document).RemoveImages},
300+
{"RemoveJavaScripts", (*Document).RemoveJavaScripts},
301+
{"PageRotate", func(doc *Document) error { return doc.PageRotate(1, RotationOn270) }},
302+
{"PageSetSize", func(doc *Document) error { return doc.PageSetSize(1, PageSizeA1) }},
303+
{"PageGrayscale", func(doc *Document) error { return doc.PageGrayscale(1) }},
304+
{"PageAddText", func(doc *Document) error { return doc.PageAddText(1, "Page-level text") }},
305+
{"PageReplaceText", func(doc *Document) error {
306+
_ = doc.PageAddText(1, "Replace me")
307+
return doc.PageReplaceText(1, "Replace", "Changed")
308+
}},
309+
{"PageAddPageNum", func(doc *Document) error { return doc.PageAddPageNum(1) }},
310+
{"PageAddTextHeader", func(doc *Document) error { return doc.PageAddTextHeader(1, "Page Header") }},
311+
{"PageAddTextFooter", func(doc *Document) error { return doc.PageAddTextFooter(1, "Page Footer") }},
312+
{"PageRemoveAnnotations", func(doc *Document) error { return doc.PageRemoveAnnotations(1) }},
313+
{"PageRemoveHiddenText", func(doc *Document) error { return doc.PageRemoveHiddenText(1) }},
314+
{"PageRemoveImages", func(doc *Document) error { return doc.PageRemoveImages(1) }},
315+
}
316+
317+
for _, test := range organizeFunctions {
318+
t.Run(test.name, func(t *testing.T) {
319+
doc, err := New()
320+
if err != nil {
321+
t.Fatalf("New(): %v", err)
322+
}
323+
defer doc.Close()
324+
325+
if err := doc.PageAdd(); err != nil {
326+
t.Fatalf("PageAdd(): %v", err)
327+
}
328+
329+
if err := test.fn(doc); err != nil {
330+
t.Errorf("%s(): %v", test.name, err)
331+
}
332+
333+
outputPath := fmt.Sprintf("%s/output.pdf", t.TempDir())
334+
if err := doc.SaveAs(outputPath); err != nil {
335+
t.Errorf("SaveAs(): %v", err)
336+
}
337+
338+
info, err := os.Stat(outputPath)
339+
if err != nil {
340+
t.Errorf("Stat(%s): %v", outputPath, err)
341+
} else {
342+
assert_ne(t, int64(0), info.Size())
343+
}
344+
})
345+
}
346+
}
347+
348+
func TestRepair(t *testing.T) {
349+
350+
tmpDir := t.TempDir()
351+
filePath := fmt.Sprintf("%s/input.pdf", tmpDir)
352+
353+
doc, err := New()
354+
if err != nil {
355+
t.Fatalf("New(): %v", err)
356+
}
357+
defer doc.Close()
358+
359+
if err := doc.PageAdd(); err != nil {
360+
t.Fatalf("PageAdd(): %v", err)
361+
}
362+
if err := doc.SaveAs(filePath); err != nil {
363+
t.Fatalf("SaveAs(%s): %v", filePath, err)
364+
}
365+
366+
reopenDoc, err := Open(filePath)
367+
if err != nil {
368+
t.Fatalf("Open(%s): %v", filePath, err)
369+
}
370+
defer reopenDoc.Close()
371+
372+
if err := reopenDoc.Repair(); err != nil {
373+
t.Errorf("Repair(): %v", err)
374+
}
375+
}

0 commit comments

Comments
 (0)