Skip to content

Commit

Permalink
Export as transparent PNG (#557)
Browse files Browse the repository at this point in the history
* added boolean transparent parameter to exportAsImage

* Updating package version

* update API.md

Co-authored-by: JamesTron <[email protected]>

* Updating package.json

* Updating package version

* default argument for the DocumentView method

* Updating package.json

* Updating package version

* Updating package version

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: JamesTron <[email protected]>
  • Loading branch information
3 people authored May 17, 2022
1 parent b63b42f commit 77552c2
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 28 deletions.
6 changes: 4 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pageNumber | int | the page to be converted; if the value does not refer to a va
dpi | double | the output image resolution
exportFormat | string | one of [`Config.ExportFormat`](./src/Config/Config.ts) constants
filePath | string | local file path to pdf
transparent | boolean | (only relevant when exported as PNG) whether the background of the image is transparent or the color of the PDF page (typically white)

Returns a Promise.

Expand All @@ -232,7 +233,7 @@ Name | Type | Description
resultImagePath | string | the temp path of the created image, user is responsible for clean up the cache

```js
RNPdftron.exportAsImage(1, 92, Config.ExportFormat.BMP, "/sdcard/Download/red.pdf").then((resultImagePath) => {
RNPdftron.exportAsImage(1, 92, Config.ExportFormat.BMP, "/sdcard/Download/red.pdf", false).then((resultImagePath) => {
console.log('export', resultImagePath);
});
```
Expand Down Expand Up @@ -3719,6 +3720,7 @@ Name | Type | Description
pageNumber | int | the page to be converted; if the value does not refer to a valid page number, the file path will be undefined
dpi | double | the output image resolution
exportFormat | string | one of the [`Config.ExportFormat`](./src/Config/Config.ts) constants
transparent | boolean | (only relevant when exported as PNG) whether the background of the image is transparent or opaque white

Returns a Promise.

Expand All @@ -3727,7 +3729,7 @@ Name | Type | Description
path | string | the temp path of the created image, user is responsible for clean up the cache

```js
this._viewer.exportToImage(1, 92, Config.ExportFormat.BMP).then((path) => {
this._viewer.exportAsImage(1, 92, Config.ExportFormat.BMP, false).then((path) => {
console.log('export', path);
});
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1146,9 +1146,9 @@ public void run() {
}

@ReactMethod
public void exportAsImage(final int tag, int pageNumber, double dpi, String exportFormat, final Promise promise) {
public void exportAsImage(final int tag, int pageNumber, double dpi, String exportFormat, boolean transparent, final Promise promise) {
try {
String result = mDocumentViewInstance.exportAsImage(tag, pageNumber, dpi, exportFormat);
String result = mDocumentViewInstance.exportAsImage(tag, pageNumber, dpi, exportFormat, transparent);
promise.resolve(result);
} catch (Exception ex) {
promise.reject(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ public void pdfFromOfficeTemplate(final String docxPath, final ReadableMap json,
}

@ReactMethod
public void exportAsImage(int pageNumber, double dpi, String exportFormat, final String filePath, final Promise promise) {
public void exportAsImage(int pageNumber, double dpi, String exportFormat, final String filePath, boolean transparent, final Promise promise) {
try {
PDFDoc doc = new PDFDoc(filePath);
doc.lockRead();
String imagePath = ReactUtils.exportAsImageHelper(doc, pageNumber, dpi, exportFormat);
String imagePath = ReactUtils.exportAsImageHelper(doc, pageNumber, dpi, exportFormat, transparent);
doc.unlockRead();
promise.resolve(imagePath);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ public static JSONArray convertArrayToJson(ReadableArray readableArray) throws J
return array;
}

public static String exportAsImageHelper(PDFDoc doc, int pageNumber, double dpi, String exportFormat) {
public static String exportAsImageHelper(PDFDoc doc, int pageNumber, double dpi, String exportFormat, boolean transparent) {
PDFDraw draw = null;
try {
draw = new PDFDraw();
draw.setDPI(dpi);
draw.setPageTransparent(transparent);
if (pageNumber <= doc.getPageCount() && pageNumber >= 1) {
Page pg = doc.getPage(pageNumber);
String ext = "png";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1216,10 +1216,10 @@ public void selectAll(int tag) throws PDFNetException {
}
}

public String exportAsImage(final int tag, int pageNumber, double dpi, String exportFormat) throws PDFNetException {
public String exportAsImage(final int tag, int pageNumber, double dpi, String exportFormat, boolean transparent) throws PDFNetException {
DocumentView documentView = mDocumentViews.get(tag);
if (documentView != null) {
return documentView.exportAsImage(pageNumber, dpi, exportFormat);
return documentView.exportAsImage(pageNumber, dpi, exportFormat, transparent);
} else {
throw new PDFNetException("", 0L, getName(), "exportAsImage", "Unable to find DocumentView.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4545,14 +4545,14 @@ public void selectAll() {
}
}

public String exportAsImage(int pageNumber, double dpi, String exportFormat) {
public String exportAsImage(int pageNumber, double dpi, String exportFormat, boolean transparent) {
PDFViewCtrl pdfViewCtrl = getPdfViewCtrl();
if (pdfViewCtrl != null) {
boolean shouldUnlockRead = false;
try {
pdfViewCtrl.docLockRead();
shouldUnlockRead = true;
return ReactUtils.exportAsImageHelper(pdfViewCtrl.getDoc(), pageNumber, dpi, exportFormat);
return ReactUtils.exportAsImageHelper(pdfViewCtrl.getDoc(), pageNumber, dpi, exportFormat, transparent);
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface RNPdftron {
pdfFromOffice(docxPath: string, options: {applyPageBreaksToSheet?: boolean, displayChangeTracking?: boolean, excelDefaultCellBorderWidth?: number,
excelMaxAllowedCellCount?: number, locale?: string}) : Promise<string>;
pdfFromOfficeTemplate(docxPath: string, json: object) : Promise<string>;
exportAsImage(pageNumber: number, dpi: number, exportFormat: Config.ExportFormat, filePath: string) : Promise<string>;
exportAsImage(pageNumber: number, dpi: number, exportFormat: Config.ExportFormat, filePath: string, transparent: boolean) : Promise<string>;
clearSavedViewerState() : Promise<void>;
}

Expand Down
2 changes: 1 addition & 1 deletion ios/RNPdftron.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@class PTPDFDoc;
@interface RNPdftron : NSObject <RCTBridgeModule>

+(NSString*)exportAsImageHelper:(PTPDFDoc*)doc pageNumber:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)imageFormat;
+(NSString*)exportAsImageHelper:(PTPDFDoc*)doc pageNumber:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)imageFormat transparent:(BOOL)transparent;

@end

13 changes: 10 additions & 3 deletions ios/RNPdftron.m
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,17 @@ - (void)setPassword:(NSString *)password onPDFDoc:(PTPDFDoc *)pdfDoc
}
}

RCT_EXPORT_METHOD(exportAsImage:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat filePath:(NSString*)filePath resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
RCT_EXPORT_METHOD(exportAsImage:(int)pageNumber
dpi:(int)dpi
exportFormat:(NSString*)exportFormat
filePath:(NSString*)filePath
transparent:(BOOL)transparent
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
@try {
PTPDFDoc * doc = [[PTPDFDoc alloc] initWithFilepath:filePath];
NSString * resultImagePath = [RNPdftron exportAsImageHelper:doc pageNumber:pageNumber dpi:dpi exportFormat:exportFormat];
NSString * resultImagePath = [RNPdftron exportAsImageHelper:doc pageNumber:pageNumber dpi:dpi exportFormat:exportFormat transparent:transparent];

resolve(resultImagePath);
}
Expand All @@ -223,7 +229,7 @@ - (NSError *)errorFromException:(NSException *)exception
}];
}

+(NSString*)exportAsImageHelper:(PTPDFDoc*)doc pageNumber:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat
+(NSString*)exportAsImageHelper:(PTPDFDoc*)doc pageNumber:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat transparent:(BOOL)transparent
{
NSString * resultImagePath = nil;
BOOL shouldUnlock = NO;
Expand All @@ -233,6 +239,7 @@ +(NSString*)exportAsImageHelper:(PTPDFDoc*)doc pageNumber:(int)pageNumber dpi:(i

if (pageNumber <= [doc GetPageCount] && pageNumber >= 1) {
PTPDFDraw *draw = [[PTPDFDraw alloc] initWithDpi:dpi];
[draw SetPageTransparent:transparent];
NSString* tempDir = NSTemporaryDirectory();
NSString* fileName = [NSUUID UUID].UUIDString;
resultImagePath = [tempDir stringByAppendingPathComponent:fileName];
Expand Down
2 changes: 1 addition & 1 deletion ios/RNTPTDocumentView.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ static NSString * const PTSignaturesManager_signatureDirectory = @"PTSignaturesM

- (NSMutableArray<NSDictionary *> *)getAllFieldsForDocumentViewTag:(int)pageNumber;

- (NSString *)exportAsImage:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat;
- (NSString *)exportAsImage:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat transparent:(BOOL)transparent;

- (nullable NSString *)exportAnnotationsWithOptions:(NSDictionary *)options;

Expand Down
4 changes: 2 additions & 2 deletions ios/RNTPTDocumentView.m
Original file line number Diff line number Diff line change
Expand Up @@ -4830,10 +4830,10 @@ - (NSString *) getDocumentPath {

#pragma mark - Export as image

- (NSString *)exportAsImage:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat
- (NSString *)exportAsImage:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat transparent:(BOOL)transparent
{
PTPDFDoc * doc = [self.currentDocumentViewController.pdfViewCtrl GetDoc];
return [RNPdftron exportAsImageHelper:doc pageNumber:pageNumber dpi:dpi exportFormat:exportFormat];
return [RNPdftron exportAsImageHelper:doc pageNumber:pageNumber dpi:dpi exportFormat:exportFormat transparent:transparent];
}

#pragma mark - Tabs
Expand Down
2 changes: 1 addition & 1 deletion ios/RNTPTDocumentViewManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

- (NSString *)getDocumentPathForDocumentViewTag:(NSNumber *)tag;

- (NSString*) exportAsImageForDocumentViewTag:(NSNumber*)tag pageNumber:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat;
- (NSString*) exportAsImageForDocumentViewTag:(NSNumber*)tag pageNumber:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat transparent:(BOOL)transparent;

- (int)getPageCountForDocumentViewTag:(NSNumber *)tag;

Expand Down
4 changes: 2 additions & 2 deletions ios/RNTPTDocumentViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -998,11 +998,11 @@ - (NSString *)getDocumentPathForDocumentViewTag:(NSNumber *)tag
}
}

- (NSString *)exportAsImageForDocumentViewTag:(NSNumber *)tag pageNumber:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat
- (NSString *)exportAsImageForDocumentViewTag:(NSNumber *)tag pageNumber:(int)pageNumber dpi:(int)dpi exportFormat:(NSString*)exportFormat transparent:(BOOL)transparent
{
RNTPTDocumentView *documentView = self.documentViews[tag];
if (documentView) {
return [documentView exportAsImage:pageNumber dpi:dpi exportFormat:exportFormat];
return [documentView exportAsImage:pageNumber dpi:dpi exportFormat:exportFormat transparent:transparent];
} else {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Unable to find DocumentView for tag" userInfo:nil];
return nil;
Expand Down
3 changes: 2 additions & 1 deletion ios/RNTPTDocumentViewModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ - (NSError *)errorFromException:(NSException *)exception
pageNumber:(int)pageNumber
dpi:(int)dpi
exportFormat:(NSString*)exportFormat
transparent:(BOOL)transparent
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
@try {
NSString *path = [[self documentViewManager] exportAsImageForDocumentViewTag:tag pageNumber:pageNumber dpi:dpi exportFormat:exportFormat];
NSString *path = [[self documentViewManager] exportAsImageForDocumentViewTag:tag pageNumber:pageNumber dpi:dpi exportFormat:exportFormat transparent:transparent];
resolve(path);
}
@catch (NSException *exception) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/DocumentView/DocumentView.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,10 +963,10 @@ export class DocumentView extends PureComponent {
}
return Promise.resolve();
};
exportAsImage = (pageNumber, dpi, exportFormat) => {
exportAsImage = (pageNumber, dpi, exportFormat, transparent = false) => {
const tag = findNodeHandle(this._viewerRef);
if (tag != null) {
return DocumentViewManager.exportAsImage(tag, pageNumber, dpi, exportFormat);
return DocumentViewManager.exportAsImage(tag, pageNumber, dpi, exportFormat, transparent);
}
return Promise.resolve();
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-pdftron",
"title": "React Native Pdftron",
"version": "3.0.2-beta.98",
"version": "3.0.2-beta.99",
"description": "React Native Pdftron",
"main": "./lib/index.js",
"typings": "index.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/DocumentView/DocumentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1047,10 +1047,10 @@ export class DocumentView extends PureComponent<DocumentViewProps, any> {
return Promise.resolve();
}

exportAsImage = (pageNumber: number, dpi: number, exportFormat: Config.ExportFormat): Promise<void | string> => {
exportAsImage = (pageNumber: number, dpi: number, exportFormat: Config.ExportFormat, transparent: boolean = false): Promise<void | string> => {
const tag = findNodeHandle(this._viewerRef);
if (tag != null) {
return DocumentViewManager.exportAsImage(tag, pageNumber, dpi, exportFormat);
return DocumentViewManager.exportAsImage(tag, pageNumber, dpi, exportFormat, transparent);
}
return Promise.resolve();
}
Expand Down

0 comments on commit 77552c2

Please sign in to comment.